标签: fluent-entity-framework

一个与HasForeignKey为零或一

我有两个型号:

public class Person
{
    public virtual int Id { get; set; }
    public virtual Employee Employee { get; set; } // optional
}

public class Employee
{
    public virtual int Id { get; set; }
    public virtual int PersonId { get; set; }

    public virtual Person Person {get; set; } // required
}

public class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
    public EmployeeConfiguration()
    {
        Property(e=>e.PersonId) // I need this property mapped
            .HasColumnName("person_id")
            .HasColumnType("int");
    }
}
Run Code Online (Sandbox Code Playgroud)

我想使用流畅的映射来映射它们.Employee表的列'person_id'是不可为空的.我试过以下:

HasRequired(e => e.Person)
    .WithOptional(p => …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework fluent-entity-framework

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

在HasOptional()中映射外键.实体框架6中的WithOptionalDependent()关系

我在Entity Framework 6.1.3中有以下数据模型:

using System.Data.Entity;

public class Student
{
    public int Id { get; set; }
    public virtual Contact Contact { get; set; }
}

public class Contact
{
    public int Id { get; set; }
    public virtual Student Student { get; set; }
}

public class MyContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder builder)
    {
        builder.Entity<Contact>()
            .HasOptional(x => x.Student)
            .WithOptionalDependent(x => x.Contact)
            .WillCascadeOnDelete(true);
    }
}

public static class Program
{
    private static void Main()
    {
        Database.SetInitializer(new DropCreateDatabaseAlways<MyContext>());

        using …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework fluent-entity-framework

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

EF Core fluent映射到内部对象属性

我有一个包含一些属性的类.出于某些架构原因,我在我的班级中有另一个对象的实例.

简单的例子

public class MyEntity {
   public MySubEntity SubEntity {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

为此,我创建了流畅的映射,如:

builder.ToTable(MyEntity.CONST_TABLE_NAME);
builder.HasKey(m => m.Id);
builder.Property(m => m.Column1).IsRequired();
builder.Property(m => m.SubEntity.Column2).IsRequired();
Run Code Online (Sandbox Code Playgroud)

我无法将所有subEntity属性集成到我的主实体中(我的subEntity有自己的智能).我只想将我的子实体属性(不存储在单独的表中)映射到myEntity表.

最后一行抛出异常:

The expression 'm => m.SubEntity.Column2' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'.
Run Code Online (Sandbox Code Playgroud)

我该如何进行这种映射?

c# fluent-entity-framework entity-framework-core

5
推荐指数
1
解决办法
1838
查看次数

EntityFramework Core Fluent Model Builder 键和属性

好的,所以在实体框架 6 中,我会在一个语句中生成密钥和属性数据库:

modelBuilder.Entity<Function>()
                .HasKey(x => x.Id)
                .Property(x => x.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Run Code Online (Sandbox Code Playgroud)

在实体框架核心 (7) 中,这不起作用:

modelBuilder.Entity<Function>()
                .HasKey(x => x.Id)
                .Property(x => x.Id)
                .ValueGeneratedNever();
Run Code Online (Sandbox Code Playgroud)

错误:“‘KeyBuilder’不包含‘Property’的定义,并且没有接受‘KeyBuilder’类型的第一个参数的扩展方法‘Property’”:

这是否必须是如下两个单独的语句,或者有没有办法像在 EF6 中那样将其合并在一起?

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Function>()
                    .HasKey(x => x.Id);

    modelBuilder.Entity<Function>()
                    .Property(x => x.Id)
                    .ValueGeneratedNever();
}
Run Code Online (Sandbox Code Playgroud)

c# entity-framework fluent fluent-entity-framework entity-framework-core

4
推荐指数
1
解决办法
2569
查看次数

PropertyBuilder <T>不包含HasColumnType的定义

我刚刚使用EntityFramework Core和Scaffold-DbContext NuGet Package Manager Console命令构建了一堆POCO类和一个DbContext类.

它生成了一堆代码,大部分代码都很好,除了有几次调用编译器找不到的HasColumnType和HasName方法.不幸的是,VS2017也没有帮我找到它们.我通过NuGet包管理器安装了EntityFramework Core,我认为所有依赖项都已正确安装,但似乎并非如此.我已经尝试使用Google搜索HasColumnType的命名空间但无法找到它.

有人可以告诉我我缺少什么命名空间或NuGet包吗?

c# fluent-entity-framework entity-framework-core

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