EF CF:遗留数据库的复杂类型

发布于 2024-10-27 01:41:32 字数 442 浏览 1 评论 0原文

我成功地映射了我的复杂类型,如下所示:

modelBuilder
     .ComplexType<Name>()
     .Property(name => name.First)
     .HasColumnName("firstNameColumn");

modelBuilder
     .ComplexType<Name>()
     .Property(name => name.Last)
     .HasColumnName("lastNameColumn");

到目前为止一切顺利。但请注意,我们没有指定任何实体类型。如果我们想为包含“firstN”和“lastN”列的表映射相同的复杂类型怎么办?我尝试过 EntityTypeConfiguration<>但不允许您在那里指定复杂类型。最后,看起来复杂类型是全局定义的。

I successfully mapped my complex type like this:

modelBuilder
     .ComplexType<Name>()
     .Property(name => name.First)
     .HasColumnName("firstNameColumn");

modelBuilder
     .ComplexType<Name>()
     .Property(name => name.Last)
     .HasColumnName("lastNameColumn");

So far so good. But notice that we do not specify any entity type. What if we want to map the same complext type also for a table with columns "firstN" and "lastN"? I tried EntityTypeConfiguration<> but you are not allowed to specify complex types there. Finally it looks like that complexTypes are defined globally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-11-03 01:41:32

您还可以在实体级别自定义复杂类型列名称,如下所示:

public class User
{
    public int UserId { get; set; }
    public Name NameInfo { get; set; }
}

public class Customer
{
    public int CustomerId { get; set; }
    public Name NameInfo { get; set; }
}

[ComplexType]
public class Name
{
    public string First { get; set; }
    public string Last { get; set; }
}

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Name>()
                    .Property(name => name.First)
                    .HasColumnName("firstNameColumn");

        modelBuilder.ComplexType<Name>()
                    .Property(name => name.Last)
                    .HasColumnName("lastNameColumn");

        // Here is how can customize the column names at the entity level:
        modelBuilder.Entity<Customer>().Property(u => u.NameInfo.First)
                                       .HasColumnName("firstN");

        modelBuilder.Entity<Customer>().Property(u => u.NameInfo.Last)
                                       .HasColumnName("lastN");
    }
}

生成的架构将为:

输入图像描述此处

此处 你可以找到另一个例子。

You can also customize the complex type columns names at the entity level, like the following:

public class User
{
    public int UserId { get; set; }
    public Name NameInfo { get; set; }
}

public class Customer
{
    public int CustomerId { get; set; }
    public Name NameInfo { get; set; }
}

[ComplexType]
public class Name
{
    public string First { get; set; }
    public string Last { get; set; }
}

public class Context : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Customer> Customers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<Name>()
                    .Property(name => name.First)
                    .HasColumnName("firstNameColumn");

        modelBuilder.ComplexType<Name>()
                    .Property(name => name.Last)
                    .HasColumnName("lastNameColumn");

        // Here is how can customize the column names at the entity level:
        modelBuilder.Entity<Customer>().Property(u => u.NameInfo.First)
                                       .HasColumnName("firstN");

        modelBuilder.Entity<Customer>().Property(u => u.NameInfo.Last)
                                       .HasColumnName("lastN");
    }
}

And the resultant schema will be:

enter image description here

Here you can find another example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文