【问题标题】:Entity Framework Code First Set Mapping实体框架代码优先集映射
【发布时间】:2014-04-15 12:49:16
【问题描述】:

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

一个用户可能有个人资料

一个配置文件必须有一个或多个常设数据值

标准数据值用于 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);
    }

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

例如people 表中的命令分隔的 ID 列表或更好的东西

谢谢

【问题讨论】:

  • 你甚至没有为你的模型发布代码。

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


【解决方案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; }
}

【讨论】:

    猜你喜欢
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 2011-04-21
    • 2012-05-18
    相关资源
    最近更新 更多