- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
很简单,我首先使用 Entity Framework 4.1 代码,我想用对模型构建器的流畅调用替换我的 [ForeignKey(..)] 属性。类似于下面的 WithRequired(..) 和 HasForeignKey(..) ,它们将显式外键属性 (CreatedBySessionId) 与关联的导航属性 (CreatedBySession) 联系在一起。但我想为一对一的关系而不是一对多这样做:
modelBuilder.Entity<..>().HasMany(..).WithRequired(x => x.CreatedBySession).HasForeignKey(x => x.CreatedBySessionId)
public class VendorApplication
{
public int VendorApplicationId { get; set; }
public int CreatedBySessionId { get; set; }
public virtual Session CreatedBySession { get; set; }
}
public class Session
{
public int SessionId { get; set; }
[ForeignKey("CurrentApplication")]
public int? CurrentApplicationId { get; set; }
public virtual VendorApplication CurrentApplication { get; set; }
public virtual ICollection<VendorApplication> Applications { get; set; }
}
public class MyDataContext: DbContext
{
public IDbSet<VendorApplication> Applications { get; set; }
public IDbSet<Session> Sessions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Session>().HasMany(x => x.Applications).WithRequired(x => x.CreatedBySession).HasForeignKey(x => x.CreatedBySessionId).WillCascadeOnDelete(false);
// Note: We have to turn off Cascade delete on Session <-> VendorApplication relationship so that SQL doesn't complain about cyclic cascading deletes
}
}
modelBuilder.Entity<Session>().HasOptional(x => x.CurrentApplication).WithOptionalDependent().Map(config => config.MapKey("CurrentApplicationId"));
最佳答案
您需要将关系映射为一对多并省略关系中的集合属性。
modelBuilder.Entity<Session>()
.HasOptional(x => x.CurrentApplication)
.WithMany()
.HasForeignKey(x => x.CurrentApplicationId)
关于entity-framework - EF 4.1 Code First ModelBuilder HasForeignKey 用于一对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480083/
我刚刚开始研究 Entity Framework 代码优先方法,我在下面编写了两种方法并且都运行良好。 请告诉我这两种方法背后的核心概念是什么,应该遵循什么? 方法 1:使用 EntityTypeCo
当前升级到 Net 6 后出现构建错误 Error CS8795 Partial method 'XXXX.OnModelCreatingPartial(ModelBuilder)' must hav
我将 EF Core 与代码优先一起使用,并且我有模型公司 public class Company { public Guid Id { get; set; } pub
有没有一种方法可以为 Required、MaxLength 和 HasColumn 组合多个属性,或者我是否需要为每个属性创建一个? 我希望能够包括多个必填字段,并为它们分配相同的 MaxLength
在我的第一个 libgdx 3D 游戏中,我现在从 createBox 切换到 createRect,仅创建可见面(如果一堵墙位于另一堵墙的左侧,它的右脸是看不见的......)。我正在创建 4 个模
我正在覆盖 OnModelCreating , 在方法中有 base.OnModelCreating(modelBuilder); 行 protected override void OnModelC
在我的 libgdx android 应用程序中,我使用以下代码创建箭头 @Override public void onAxonConnected(Pos one, Pos two) { M
我正在尝试实现一个 Multi-Tenancy 应用程序,我通过租户对象查询数据库,而不是直接脱离上下文。在我有这个之前: public User GetUserByEmail(string emai
这是 this answer 的延续. 我正在尝试使用 maven-model-builder 以编程方式访问 pom.xml 的完整有效 pom 。目前,我因需要创建 MavenResolver 对
我想即时将域类型添加到上下文中。重写 OnModelCreating ,添加类型和配置添加的类型是我假设的非常自然的方式: public MyContext : DbContext { pro
我有以下生成 3D 立方体的代码片段: ModelBuilder modelBuilder = new ModelBuilder(); box = modelBuilder.createBox(2f,
我是 LibGDX 3D 工具的新手,我想知道如何合并使用 ModelBuilder#createCylinder 创建的两个圆柱体类。我有两个模型实例: 第一个是白色圆柱体, 第二个是具有相同属性的
有many questions关于在 Entity Framework 中播种多对多关系。不过大部分都非常老了,多对多的行为有changed significantly in EFCore5 . of
我正在使用 Entity Framework 开发 ASP MVC 应用程序。我正在考虑编写代码来缓存 ModelBuilder 返回的对象(正如几个来源所推荐的那样),但后来我在 Scott Gu
好的,所以在 Entity Framework 6 中,我会在一条语句中生成 key 和属性数据库: modelBuilder.Entity() .HasKey(x =
在 EntityFramework 6.x 中,如果我们有很多 EntityConfiguration 类,那么我们可以在 OnModelCreating(ModelBuilder modelBuil
在step 7 of the ASP.MVC 3 Tutorial ,它引入了“ModelBuilder”类型。我使用的是 ASP.Net 4、MVC 3 和 EF 4.1,我在“ModelBuild
在一个新的asp.net core 3.1项目中返回上面的错误信息。我有一个根据 Text 配置的多对多关系 不确定为什么会发生这种情况,但我已将 EntityFrameworkCore 包添加到项目
我是 android 开发的新手,突然间我收到了以下信息错误:您使用的 Gradle 版本(Gradle 发行版“https://services.gradle.org/distributions/g
我想使用注释为 Entity Framework Core 中的属性设置默认值。问题是数据库没有设置默认值,因此该值没有传递到数据库层。 我想做类似 modelBuilder 的事情的 HasDefa
我是一名优秀的程序员,十分优秀!