Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

c# - Multiplicity conflicts with the referential constraint

I'm receiving the following EF error:

Agent_MailingAddress: : Multiplicity conflicts with the referential constraint in Role 'Agent_MailingAddress_Target' in relationship 'Agent_MailingAddress'. Because all of the properties in the Dependent Role are non-nullable, multiplicity of the Principal Role must be 1

It appears to throw this when it executes base.OnModelCreating(modelBuilder).

Here are my models. FWIW, Agent inherits from a User class.

public class Agent
{
    public int AgentId { get; set; }
    public int PrimaryAddressId { get; set; }
    public Address PrimaryAddress { get; set; }
    public int? MailingAddressId { get; set; }
    public Address MailingAddress { get; set; }
}

public class Address
{
    public int AddressId { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
}

I believe the issue has something to do with the fact that Agent has more than one property of type Address and possibly also because one of them is nullable. I've done some searching, but can't seem to find an answer.

I assume altering my Agent model to have a single property of type List<Address> that would use a UserAddresses lookup table would resolve the error, but I would prefer to keep the current model and not.

How can I resolve this error? Thanks in advance.

question from:https://stackoverflow.com/questions/32459263/multiplicity-conflicts-with-the-referential-constraint

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This can happen if your configuration and your model do not match.

Let's say in your db configuration you have a rule like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Agent>().HasRequired(x=>x.MailingAddress);
    //..

But in your model you say that MailingAddress is optional:

public int? MailingAddressId { get; set; }

I believe the issue has something to do with the fact that Agent has more than one property of type Address and possibly also because one of them is nullable

It's not the case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...