首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用实体框架描述同一表中记录之间的关系

如何用实体框架描述同一表中记录之间的关系
EN

Stack Overflow用户
提问于 2014-06-05 20:49:59
回答 2查看 45关注 0票数 0

在EF中,我需要描述以下关系:

一家公司可能有很多地点,比如

代码语言:javascript
复制
headquarter         <= main location 
      plant      ----+
      warehouse      |
      store-1        +----> child Locations
      store-2        |
      store-n    ----+

所以我需要一个位置模型中的mainLocationID,这样我就可以

1)给定主位置,我可以访问它的所有子位置

2)给定一个孩子的位置,我可以找到它的主要位置。

所以我试着做了以下几件事:

代码语言:javascript
复制
public class Location
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public bool flagMainLocation { get; set; }

    public int? mainLocationID { get; set; }
    public virtual ICollection<Location> ChildLocations { get; set; }
}

在我的db上下文中

代码语言:javascript
复制
public class myappContext : DbContext
{
    public myappContext() : base("myappContext")
    {
    }

    public DbSet<Location> Locations { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Location>()
                            .HasOptional(l => l.ChildLocations)
                            .WithMany()
                            .HasForeignKey(l => l.mainLocationID);
    }
}

现在我陷入困境,因为当我试图为Location类搭建一个控制器时,我会得到以下错误

代码语言:javascript
复制
"myapp.DAL.Location_ChildLocations:: Multiplicity conflicts with 
the referential constraint in Role 'Location_ChildLocations_Target' 
in relationship 'Location_ChildLocations'. Because all of the 
properties in the Dependant Role are non-nullable, multiplicity 
of the Principal Role must be '1'."

我对EF不够精通,无法解密这条错误信息。有人能告诉我这种配置有什么问题吗?

我也希望能以这种方式获得主要位置。

代码语言:javascript
复制
Location myChildLocation = db.Locations.Find(some_location_id);
Location mainLocation = myChildLocation.mainLocation;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-05 21:00:16

如果你试一试:

代码语言:javascript
复制
modelBuilder.Entity<Location>()
    .HasMany(l => l.ChildLocations)
    .WithOptional()
    .HasForeignKey(l => l.mainLocationID);
票数 1
EN

Stack Overflow用户

发布于 2014-06-06 04:25:27

好的,在BiffBaffBoff的提示下,为了到达主位置,添加了一些语法糖,我终于让它运行了:

代码语言:javascript
复制
public class Location
{
    public int id { get; set; }
    public string name { get; set; }
    public string type { get; set; }
    public bool flagMainLocation { get; set; }

    public int? mainLocationID { get; set; }
    public virtual ICollection<Location> ChildLocations { get; set; }
    public virtual Location mainLocation { get; set;}
}

public class myappContext : DbContext
{
    public myappContext() : base("myappContext")
    {
    }

    public DbSet<Location> Locations { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<Location>()
            .HasMany(l => l.ChildLocations)
            .WithOptional()
            .HasForeignKey(l => l.mainLocationID);
    }
}

参见github https://github.com/kranz/selfRefModel上的工作示例

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24070019

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档