首先在MVC中处理EF代码中的1到0.1关系 [英] handling 1 to 0.1 relationship in EF code first in MVC

查看:104
本文介绍了首先在MVC中处理EF代码中的1到0.1关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我使用EF代码第一种方法创建了一个完整的代码。



但是当我尝试使用EF添加控制器



i得到以下错误:



无法确定主要结束类型model.person和供应商之间的联系。而在设计师中,人和供应商之间的现实船是1到0..1



以下是两种模态的代码:



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 :

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>







public class CartContext : DbContext
    {
        public CartContext() : base("cartModelContainer") { }
....
     }

推荐答案

您的模型声明是错误的。请尝试如下。



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

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; }
    }





查看这篇文章了解更多信息:One-to-One Foreign关键关联


您必须使用数据注释或通过流畅的API设置主体 - 从属关联。



意味着,如果1:1使用必需注释或1 - 0..1需要可选。



参考



http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in -entity-fr [ ^ ]
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[^]


这篇关于首先在MVC中处理EF代码中的1到0.1关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆