实体框架代码第一集映射

时间:2014-04-15 12:49:17

标签: c# database entity-framework ef-code-first foreign-key-relationship

实体框架代码首先是设置我的映射错误。映射问题如下所示:

一个用户可能有个人资料

个人资料必须包含一个或多个常设数据值

常用数据值用于3/4个不同的表格,无法更改,例如无法在该表格中添加新列。

它想要做的映射如下:

    public override void Up()
    {
        AddColumn("dbo.StandingDatas", "Person_Id", c => c.Int());
        CreateIndex("dbo.StandingDatas", "Person_Id");
        AddForeignKey("dbo.StandingDatas", "Person_Id", "dbo.People", "Id");
    }

我希望映射驻留在People表中,这样DA更容易控制和报告。

如何告诉enity框架在People表上托管我的一对多关系而不是站立数据表?

如果我将其从一人一对一更改为常设数据,那么效果会更好。

   public override void Up()
    {
        AddColumn("dbo.People", "Therapies_Id", c => c.Int(nullable: false));
        CreateIndex("dbo.People", "Therapies_Id");
        AddForeignKey("dbo.People", "Therapies_Id", "dbo.StandingDatas", "Id", cascadeDelete: true);
    }

我更喜欢这样的东西,但对于一对多。

e.g。人员表中的一个命令serpated Ids列表或更好的东西

由于

1 个答案:

答案 0 :(得分:1)

不清楚你的架构是什么,所以在你的第一段中描述你的poco可以有以下注释:

public class User
{
    [Key,ForeignKey("Profile")]
    public int Id { get; set; }
    public Profile Profile { get; set; }
}

public class Profile
{
    [Key]
    public int Id { get; set; }
    public virtual User ProfileOwner { get; set; }
    public virtual ICollection<StandingData> DataCollection { get; set; } 
}

public class StandingData
{
    [Key]
    public int Id { get; set; }

    public int ProfileId { get; set; }

    [ForeignKey("ProfileId")]
    public virtual Profile Profile { get; set; }
}