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

c# entity-framework fluent-interface

Question

In Visual Studio, I've constructed an entity data model. I currently have a file with SQL queries and Model-generated C# classes.

Question:

Classes are produced without code behind or annotations (Fluent API). Is it all right? When I attempted to launch my application, an exception was raised:

The relationship between the types "Runnection.Models.Address" and "Runnection.Models.User" cannot be determined at its primary end. The relationship fluent API or data annotations must be used to explicitly configure the principal end of this association.

I've read that using Fluent API with "Model First" is not possible. What then 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

/This technique is not used by Model First.

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);
    }
1
14
5/6/2014 9:54:55 PM

Popular Answer

In a one-to-one relationship, the principle must be identified.

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 an FK constraint, EF is informed that the User (the principal) must exist before the Address.

MSDN has further information.

See this SO response as well.


revised based on comments


Choose the association from the designer (line between Users & Address). Press the [...] on Referential Constraint button in the properties panel (or double click the line). the Principal to User setting.


30
5/23/2017 11:54:53 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow