Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
Hi

I have created a complete code using EF code first approach .

but when i try to add controller using EF

i get the following error that :

unable to determine principal end of a assiciationbetween the types model.person and supplier. while in the designer the realtion ship between the person and supplier is 1 to 0..1

following is the code of both modals :

C#
public class Supplier
    {
        public int ID { get; set; }
        public virtual Person Person { get; set; }
        public virtual ICollection<SupplierItem> Items { get; set; }

    }

 public class Person
    {
        public int ID { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public string Cnic { get; set; }
        public string Contact { get; set; }
        public DateTime DateAdded { get; set; }
        public virtual Supplier Supplier { get; set; }
    }



I have also tried with virtual of supplier and person object in classes.


my connectoin string :
<pre lang="c#"><connectionStrings>
    <add name="cartModelContainer" connectionString="metadata=res://*/Models.cartModel.csdl|res://*/Models.cartModel.ssdl|res://*/Models.cartModel.msl;provider=System.Data.SqlClient;provider connection string="data source=SHAN\SHAN;initial catalog=Store;persist security info=True;user id=sa;password=123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ShoppingCart-20140101195349;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ShoppingCart-20140101195349.mdf" />
  </connectionStrings>


C#
public class CartContext : DbContext
    {
        public CartContext() : base("cartModelContainer") { }
....
     }
Posted
Comments
JoCodes 5-Jan-14 8:10am    
Have you used any Fluent API for model association?
shanalikhan 5-Jan-14 8:50am    
no i havent used any. How to use fluent API

Your declaration of models are wrong.Try is as below.

C#
public class Supplier
    {
        public int ID { get; set; }
        public virtual ICollection<SupplierItem> Items { get; set; }

    }



 public class Person
    {
        public int ID { get; set; }
        public string FName { get; set; }
        public string LName { get; set; }
        public string Cnic { get; set; }
        public string Contact { get; set; }
        public DateTime DateAdded { get; set; }

        public virtual Supplier Supplier { get; set; }
    }


Check this article for more info : One-to-One Foreign Key Associations
 
Share this answer
 
Comments
shanalikhan 5-Jan-14 8:38am    
thanks but now i started to created controller i am getting the error as
Unable to retrieve metadata for models.Person
You have to set the Principal - Dependent associations using either Data Annotations or through fluent API.

Means, if its 1:1 use Required annotation or 1 - 0..1 need Optional.

Refer

http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr[^]
 
Share this answer
 
v2
Comments
shanalikhan 5-Jan-14 8:51am    
i have set the supplier class as :
public class Supplier
{
[Key, ForeignKey("Person")]
public int ID { get; set; }
public Person Person { get; set; }
public virtual ICollection<supplieritem> Items { get; set; }

}

but getting same error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900