Unable to determine the principal end of an association - Entity Framework Model First
Asked Answered
S

2

14

I have created Entity Data Model in Visual Studio. Now I have file with SQL queries and C# classes generated from Model.

Question:

Classes are generated without annotations or code behind (Fluent API). Is it OK? I tried to run my application but exception was thrown:

Unable to determine the principal end of an association between the types 'Runnection.Models.Address' and 'Runnection.Models.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

I read that I can not use Fluent API with "Model First". So what can I do?

Code:

User

public partial class User
{
    public User()
    {
        this.Events = new HashSet<Event>();
        this.CreatedEvents = new HashSet<Event>();
    }

    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Photo { get; set; }
    public int EventId { get; set; }
    public string Nickname { get; set; }
    public OwnerType OwnerType { get; set; }
    public NetworkPlaceType PlaceType { get; set; }

    public virtual ICollection<Event> Events { get; set; }
    public virtual Address Address { get; set; }
    public virtual ICollection<Event> CreatedEvents { get; set; }
    public virtual Owner Owner { get; set; }
}

Address

public partial class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string StreetNumber { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

    public virtual User User { get; set; }
}

Context

//Model First does not use this method

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>().HasRequired(address => address.User)
                                   .WithRequiredDependent();
        modelBuilder.Entity<User>().HasRequired(user => user.Address)
                                   .WithRequiredPrincipal();

        base.OnModelCreating(modelBuilder);
    }
Swain answered 6/5, 2014 at 21:54 Comment(4)
Entity framework is not fond of 1-1 relationships. It probably wants you to mark one of those as many (I know it is a workaround, but this is just a suggestion).Margoriemargot
I don't understand why 1-1 is not possible with Model First. You can see in OnModelCreating, that I can define 1-1 in case that I have used Code-First.Swain
Database table is generated from Model, and There are specified foreign keys, why should I specify it in classes again. I though it will be generated from my Model. I must define associations twice, in model and in code.Swain
Isn't this question Code First?Hermon
R
30

You have to specify the principal in a one-to-one relationship.

public partial class Address
{
    [Key, ForeignKey("User")]
    public int Id { get; set; }
    public string Street { get; set; }
    public string StreetNumber { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public string Country { get; set; }

    public virtual User User { get; set; }
}

By specifying a FK constraint, EF knows the User must exists first (the principal) and the Address follows.

Further reading at MSDN.

Also, see this SO answer.


Updated from comments


In the designer, select the association (line between Users & Address). On the properties window, hit the button with the [...] on Referential Constraint (or double click the line). Set the Principal as User.


Radiative answered 6/5, 2014 at 22:5 Comment(6)
Yes I know It is a option. But code is auto-generated from model. In case that Model will be changed, I must specify annotation to class again.Swain
Ok, answer is updated. Just double click the line between the entities and set the principal in the window that opens.Radiative
Thank you, but it does not help because Referential Constraint was already defined :(.Swain
I'm sorry that I can't be be of further help. Hopefully someone else can help answer this. In the mean time, you may want to try a 0..1 mapping. You may also want to consider a microORM like SO's own Dapper.NET. A bit more typing, but less unpredictable behavior and better performance.Radiative
"By specifying a FK constraint, EF knows the User must exists first (the principal) and the Address follows." thank you thank you thank you!Downtoearth
@Swain Why wasn't this marked as the accepted answer? You could drop and re-create your constraint and this would be your solution. Regardless, this is how a 1-1 is defined in EF and would help just about anyone else with the same question.Cockswain
M
0

Error: Had same error of "Unable to determine the principal end of an association between the types 'Providence.Common.Data.Batch' and 'Providence.Common.Data.Batch'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.".

HOWEVER, note that this is the SAME table.

Cause: My database was MS SQL Server. Unfortunately when MS SQL Server's Management Studio adds foreign keys, it adds the default foreign key as Batch ID column of Batch table linking back to itself. You as developer are suppose to pick another table and id to truly foreign key to, but if you fail to it will still allow entry of the self referencing FK.

Solution: Solution was to delete the default FK.

Cause 2: Another situation is that the current table may be fixed but the old historical image of the table when the EF's edmx was done had the default FK.

Solution 2: is to delete the table from the Model Browser's Entity Types list and click "yes" and then "Update Model from the Database" again.

Moorer answered 22/9, 2017 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.