简体   繁体   English

实体框架:在模型类中创建没有属性的数据库列

[英]Entity Framework: Create database column without property in model class

Is it possible to tell Entity Framework via fluent mapping API to add a column to a specific table without a corresponding property in a model class? 是否可以通过流畅的映射API告诉Entity Framework将列添加到特定的表而没有模型类中的相应属性?

Yes, I can achieve that by executing SQL script in a migration, but I would prefer to have this specified in a model configuration rather than in migrations. 是的,我可以通过在迁移中执行SQL脚本来实现这一点,但我更希望在模型配置中而不是在迁移中指定它。

I've been looking into this, admittedly to get around the problem of no native value-object(complex properties) handling until after release of EF core. 我一直在研究这个问题,不可否认的是,在EF核心版本发布之前解决了没有本机价值对象(复杂属性)处理的问题。

Shadow properties are a way of specifying that a migration generated from this context should add a column to the database. 影子属性是一种指定从此上下文生成的迁移应向数据库添加列的方法。 Specifically see my example: 具体看我的例子:

// In DbContext-inheriting class:
protected override void OnModelCreating(ModelBuilder builder)
{
    // Ignore the model property that holds my valueobject -
    // (a complex type encapsulating geolocation latitude/longitude)
    var entityBuilder = builder.Entity<Item>()
        .Ignore(n => n.Location);

    // Add shadow properties that are appended to the table in the DB
    entityBuilder.Property<string>("Latitude");
    entityBuilder.Property<string>("Longitude");

    base.OnModelCreating(builder);
}

This generates the migration table-creation statement as follows: 这将生成迁移表创建语句,如下所示:

migrationBuilder.CreateTable(
    name: "Items",
    columns: table => new
    {
        Key = table.Column<string>(nullable: false),
        Latitude = table.Column<double>(nullable: false),
        Longitude = table.Column<double>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Items", x => x.Key);
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在模型中声明一个属性,该属性不会因Entity Framework迁移而在数据库中作为列生成 - Declaring a property in a model that will not get generated as a column in the database by Entity Framework migrations 将数据库列映射到常量值,而不需要实体类中的属性 - Map database column to constant value without the need for a property in the entity class 实体框架模型类的导航属性 - Entity framework model class navigation property 实体框架数据库第一列未显示在模型中 - Entity Framework Database First Column not showing in model 首先创建数据库(如果存在) - Create database if exists Entity Framework Model First 实体框架将属性添加到部分类中,该部分类将从数据库中获取模型或模型列表 - Entity Framework add property to partial class that will get model or list of models from database 如何在更新数据库后将属性添加到实体框架 model class 而不会丢失它们 - How to add attributes to entity framework model class without losing them after update database 如何最好地手动使用Entity Framework Code First模型类处理现有数据库列映射? - How best to handle existing database column mappings with an Entity Framework Code First model class manually? 如何创建EntitySet或Model而不在数据库中创建相应的表 - Entity Framework - How to create a EntitySet or Model without creating corresponding table in database - Entity Framework 实体框架数据库模型 - Entity Framework Database Model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM