gpt4 book ai didi

c# - 如何在 Entity Framework 中使用流利的 api 为一对零指定外键属性..一个关系

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:44 25 4
gpt4 key购买 nike

我有一个表User 和另一个表Company用户可以注册零个或一个公司。

User (1)---> (0..1) Company

我的用户类别:

public class User {
public string Id {get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

public string FullName
{
get { return FirstName + " " + LastName; }
}

//Relations
public virtual Company Company { get; set; }

}

我的公司类(class)是:

public class Company {
public int Id { get; set; }
public string CompanyName { get; set; }
public string TaxNumber { get; set; }
public string TaxOffice { get; set; }
public string OfficeTel { get; set; }
public string FaxNumber { get; set; }
public string WebSite { get; set; }
public string Address { get; set; }
public string About { get; set; }

//keys
public int CityId { get; set; }
public int StateId { get; set; }
public string UserId { get; set; }

//relations
public City City { get; set; }
public State State { get; set; }
public User User { get; set; }
}

公司使用的fluent api是这样的:

public class CompanyConfiguration : EntityTypeConfiguration<Company>
{
public CompanyConfiguration()
{
this.HasRequired(x => x.User)
.WithOptional(x => x.Company);

this.HasRequired(x => x.City)
.WithMany(x => x.Companies).HasForeignKey(x => x.CityId).WillCascadeOnDelete(false);

this.HasRequired(x => x.State)
.WithMany(x => x.Companies).HasForeignKey(x => x.StateId).WillCascadeOnDelete(false);

this.Property(x => x.Address).HasMaxLength(400);
this.Property(x => x.CompanyName).HasMaxLength(100).IsRequired();
this.Property(x => x.FaxNumber).HasMaxLength(20);
this.Property(x => x.OfficeTel).HasMaxLength(20);
this.Property(x => x.TaxNumber).HasMaxLength(20).IsRequired();
this.Property(x => x.TaxOffice).HasMaxLength(50).IsRequired();
this.Property(x => x.WebSite).HasMaxLength(200);
}
}

在我运行 Add-Migration 后,我期望的是 UserId 用作 CompanyUser 的外键> 表,但是 Entity Framework 迁移生成的是:

CreateTable(
"dbo.Companies",
c => new
{
Id = c.Int(nullable: false, identity: true),
CompanyName = c.String(nullable: false, maxLength: 100),
TaxNumber = c.String(nullable: false, maxLength: 20),
TaxOffice = c.String(nullable: false, maxLength: 50),
OfficeTel = c.String(maxLength: 20),
FaxNumber = c.String(maxLength: 20),
WebSite = c.String(maxLength: 200),
Address = c.String(maxLength: 400),
About = c.String(),
CityId = c.Int(nullable: false),
StateId = c.Int(nullable: false),
UserId = c.String(),
User_Id = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Cities", t => t.CityId)
.ForeignKey("dbo.States", t => t.StateId)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id)
.Index(t => t.CityId)
.Index(t => t.StateId)
.Index(t => t.User_Id);

问题是我如何强制 Entity Framework 使用我指定的属性作为关系的外键,原因是我的代码中经常需要公司的 userId 值,我不想使用 Company.User.Id 表达式来获取它。

注意:我使用 Entity Framework 6.1.2 和 asp.net mvc 5

最佳答案

下面是我将如何定义这种关系中的外键:

public class Company {
public int Id { get; set; }

[Required]
[Key, ForeignKey("User")]
public string UserId { get; set; }

public User User { get; set; }
}

关于c# - 如何在 Entity Framework 中使用流利的 api 为一对零指定外键属性..一个关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com