相关疑难解决方法(0)

实体框架一对一映射问题

使用VS 2010 beta 2,ASP.NET MVC.

我试图创建一个Entity框架文件,并从我的数据库中获取数据.

这些关系存在一些问题,所以我开始调整周围的事情,但是我为了简单的一对一关系而不断收到以下错误

错误1错误113:多重性在关系'FK_UserProfiles_Users'中的角色'UserProfile'中无效.由于Dependent Role属性不是关键属性,因此Dependent Role的多重性的上限必须为*.myEntities.edmx 2024

我的Users表包含与其他表的一些其他多对多关系,但是当我尝试与其他表建立一对一的关系时,会弹出该错误.

用户表

  • 用户身份
  • 用户名
  • 电子邮件

等等..

UserProfiles表

  • UserProfileID
  • UserID(用户表的FK)
  • 地点
  • 生日

entity-framework

46
推荐指数
2
解决办法
3万
查看次数

首先通过Fluent API在EF代码中实现Zero或One to Zero或One关系

我有两个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

35
推荐指数
4
解决办法
4万
查看次数

在Entity Framework 4.1 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)

code-first relationships entity-framework-4.1

11
推荐指数
1
解决办法
4675
查看次数

Entity Framework Core 零或一到零或一关系

鉴于这些类:

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)

创建对象并将它们添加到数据库时,相应表中的内容如下所示:

  • [A].BId 已设置
  • [B].AId = 空

当我使用 EF Core 检索数据时:

  • AB 已设置,A.BId 已设置
  • BA 已设置,但B.AId 为 null

我应该怎么做才能设置 B.AId?

c# entity-framework-core ef-fluent-api

3
推荐指数
1
解决办法
2894
查看次数