EntityFramework无法确定复合主键排序

时间:2015-11-03 16:26:23

标签: c# entity-framework

我尝试使用EF代码来创建一个系统,在这个系统中,您拥有一个拥有一个想法的团队,但团队也可以拥有属于其他团队的创意实体组合。我在添加迁移和让关系工作时遇到了麻烦。以下是该模型的代码:

public class Team
{
    public Team()
    {
        Portfolio = new HashSet<Portfolio>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Balance { get; set; }
    public virtual ICollection<Portfolio> Portfolio { get; set; }
    public virtual Idea Idea { get; set; }
}

public class Idea
{
    [Key, ForeignKey("Team")]
    public int TeamId { get; set; }
    public virtual Team Team { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public decimal CurrentValue { get; set; }
}

public class Portfolio
{
    public int Id { get; set; }

    public int IdeaId { get; set; }
    public Idea Idea { get; set; }

    public int TeamId { get; set; }
    public Team Team { get; set; }
}

团队和理念是1:1,这种关系很好。我认为问题在于我的投资组合与没有Id字段的想法有关,因为它与Team 1:1只有teamid。

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Ideas_dbo.Teams_TeamId". The conflict occurred in database "VHolsDaqDb", table "dbo.Teams", column 'Id'.

1 个答案:

答案 0 :(得分:0)

不确定如何重现迁移问题。 但是我确实在单元测试中重新创建了模型,并且在生成数据库时遇到了问题 无法确定类型&#39; UnitTestProject1.Portfolio&#39;的复合主键排序。使用ColumnAttribute(请参阅http://go.microsoft.com/fwlink/?LinkId=386388)...

所以我添加了&#39;列&#39;属性和模型生成良好 [栏目(订单= n )]
也许这可以帮助您修复迁移

window.setInterval(function(){
            if(fader < <?php echo count($homePageFavs);?>) {//it's not the last image so we can move right
        var newFade = fader;
        newFade++;
        $(".fader_"+fader).fadeOut( "slow", function() {
            // Animation complete.
            $(".fader_"+newFade).fadeIn( "slow", function() {
                // Animation complete
            });
        });
        fader = newFade;
    } else { // loop back to the first one
                    $(".fader_"+fader).fadeOut( "slow", function() {
            // Animation complete.
            $(".fader_1").fadeIn( "slow", function() {
                // Animation complete
            });
        });
        fader = 1;
            }
    }, 10000);
相关问题