实体框架逻辑实体错误

实体框架逻辑实体错误

本文介绍了实体框架逻辑实体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的模型中有一个Person实体。没有对应的表,没有鉴别器。我只希望它作为一些其他实体共享的基类(我想定义一个具有通用功能的 public partial class Person



所以,例如,我想要有:

  //这个类没有相应的表DB 
public partial abstract class Person
{
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName { get; set;}

public string DisplayName {get {return string.Format({0},{1},LastName,FirstName);}}
}

//这对应于dbo.Users
public partial class User:Person
{

}

//这对应于dbo 。联系
公共部分类联系人:人
{
}

所以,我创建了一个抽象实体Person,将其设置为User和Contact实体的基类,然后添加了我的属性,并为dbo.Users和dbo.Contacts设置了我的映射对于PK Id,FirstName和LastName属性。



我收到错误:

 code> 3032:从行2109开始映射片段的问题
.....正在映射到表的相同行。映射条件可用于区分这些类型的行映射到

我如何才能正常工作?

解决方案

您必须在EDMX文件中定义 Person 实体,并继承 User 从EDMX中的 Person 联系 Person 中定义的属性都不会出现在继承的实体中。然后转到映射细节并将继承的实体映射到相应的表。



请注意,主键现在必须对所有人都是唯一的 - 不仅在派生实体中。


I want to have a Person entity in my model. There is no corresponding table and no discriminator. I simply want it as a common base class for some other entities to share (I want to define a public partial class Person with common functionality.

So, for example, I want to have:

// this class has no corresponding table in the DB
public partial abstract class Person
{
    public int Id {get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}

    public string DisplayName { get { return string.Format("{0}, {1}", LastName, FirstName); } }
}

// this corresponds to dbo.Users
public partial class User : Person
{

}

// this corresponds to dbo.Contacts
public partial class Contact : Person
{
}

So, I created an abstract entity Person, set it as the base class for the User and Contact entities, then added my properties, and set my mappings for dbo.Users and dbo.Contacts for the PK Id, FirstName, and LastName properties.

I get the error:

 3032: Problem in mapping fragments starting at lines 2109
..... are being mapped to the same rows in table .... Mapping conditions can be used to distinguish the rows that these types are mapped to.

How can I get this working?

解决方案

You must define the Person entity in EDMX file and inherit both User and Contact from the Person in EDMX. None of properties defined in Person can appear in inherited entity. Then go to mapping details and map inherited entities to corresponding tables.

Be aware that primary key now must be unique for all persons - not only in derived entities.

这篇关于实体框架逻辑实体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:53