使用VS 2010 beta 2,ASP.NET MVC.
我试图创建一个Entity框架文件,并从我的数据库中获取数据.
这些关系存在一些问题,所以我开始调整周围的事情,但是我为了简单的一对一关系而不断收到以下错误
错误1错误113:多重性在关系'FK_UserProfiles_Users'中的角色'UserProfile'中无效.由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为*.myEntities.edmx 2024
我的Users表包含与其他表的一些其他多对多关系,但是当我尝试与其他表建立一对一的关系时,会弹出该错误.
用户表
等等..
UserProfiles表
我有两个POCO课程
public class Order
{
int id;
string code;
int? quotationId; //it is foreign key
public int Id{get;set;}
public string Code{get;set;}
public int? QuotationId{get;set;}
Quotation quotation;
public virtual Quotation Quotation { get; set; }
....
}
public class Quotation
{
int Id;
string Code;
public int Id{get;set;}
public string Code{get;set;}
Order order;
public virtual Order Order { get; set; }
....
}
Run Code Online (Sandbox Code Playgroud)
每个订单可以由一个或零引号组成,每个报价可能会导致一个订单,所以我有一个"一个或零"到"一个或零"关系,我怎么能在EF代码中首先通过流畅的API实现它?
c# entity-relationship entity-framework fluent ef-code-first
我想使用EF Code First在两个实体之间创建双向一对一关系.我遇到以下代码的问题.你觉得我应该怎么做?
public class User
{
public string ID { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string FirstName { get; set; }
public int ProfileID { get; set; }
public Profile Profile { get; set; }
}
public class Profile
{
public int UserID { get; set; }
public User User { get; set; }
public int ProfileID { get; set; }
public string ProfileName { get; …Run Code Online (Sandbox Code Playgroud) 鉴于这些类:
public class A
{
public int Id {get; set;}
public int? BId {get; set;}
public B B {get; set;}
}
public class B
{
public int Id {get; set;}
public int? AId {get; set;}
public A A {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
然后使用 Fluent API
modelBuilder.Entity<A>()
.HasOne(a => a.B)
.WithOne(b => b.A)
.HasForeignKey<A>(a => a.BId);
Run Code Online (Sandbox Code Playgroud)
创建对象并将它们添加到数据库时,相应表中的内容如下所示:
当我使用 EF Core 检索数据时:
我应该怎么做才能设置 B.AId?