English 中文(简体)
我为什么在获得关系财产方面受到“夫妻间多重侵犯”?
原标题:Why do I get a "relationship multiplicity violation" accessing a relation property?

我有以下几类人试图对实体表示反对:

public class ConsumerIndexItem: MappedViewModel<Consumer>
{
    public string UserName { get; set; }
    public string RoleDescription { get; set; }
    public override void MapFromEntity(Consumer entity)
    {
        base.MapFromEntity(entity);
        UserName = entity.User.UserName;
    }
}
public class Consumer: AuditableEntity
{
    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
}
public class IndexModel<TIndexItem, TEntity> : ViewModel where TEntity : new()
{
    public IndexModel()
    {
        Items = new List<TIndexItem>();            
    } 
    public List<TIndexItem> Items { get; set; }
    public virtual void MapFromEntityList(IEnumerable<TEntity> entityList)
    {
        Items = Mapper.Map<IEnumerable<TEntity>, List<TIndexItem>>(entityList);
    }
}
public class ConsumerIndexModel: IndexModel<ConsumerIndexItem, Consumer>

然而,如果我用以下代码进行测绘:

var model = new ConsumerIndexModel();
var list = _repository.List().Where(c => c.Parent == null).ToList();
model.MapFromEntityList(list);
return View(model);

on the line UserName = entity.User.UserName; in ConsumerIndexItem I get the following exception:

A relationship multiplicity constraint violation occurred: An EntityReference can have no more than one related object, but the query returned more than one related object. This is a non-recoverable error.

如果我执行<代码>?entity。 用户:UserName in the Immediate Window I获得预期用户名值。 什么可能是错误的?

问题回答

Let me explain why I had this exception and you may be able to correlate it with your own situation. I had EF Code First model mapped to the existing database. There was one-to-many relationship between two of the entities. The child table had composite primary consisting of the Id and Date. However, I missed the second segment of the primary key in my fluent map:

this.HasKey(t => t.Id);

其中一个奇怪之处是,该模式是行之有效的,但在某些情形下却成为例外,很难理解为什么。 很明显,当欧洲武装部队装上儿童实体的母子时,有不止一名家长,因为钥匙不仅有伊德,而且有日期。 这项决议将主要内容的第二部分列入:

this.HasKey(t => new { t.Id, t.Date });

The tool that helped me to pinpoint the problem was EF Power Tools, currently it is in Beta 3. The tool gives a context menu for the EF context class where one the item is View Entity Model DDL SQL. Although I could have found this just by checking the code, the tool is nice in showing how close the EF model matches the actual database.

我认为,你再次获得这一例外,因为出于某种原因,关系的多样性受到了侵犯。 就我而言,这是不正确的绘图,因为你或许是别的东西,我可以参考你的代码。

我认为,问题可能是,你假定每个用户只有一个消费者,而在数据方面却不正确。

我也存在同样的问题,因为这种关系在不断发展,我把这种关系变成了一体。





相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

热门标签