简体   繁体   English

如何使用实体框架代码优先将 C# int 映射到 SqlServer tinyint?

[英]How do I map a C# int to a SqlServer tinyint using Entity Framework Code First?

I have a POCO model class and an existing DB table, neither of which I am able to change I am using Entity Framework 6 and the Fluent API.我有一个 POCO 模型类和一个现有的 DB 表,我无法更改这两个表,我使用的是 Entity Framework 6 和 Fluent API。

The model class has a CountryId of 'int'.模型类的 CountryId 为“int”。 However, in the database table, the CtryId is a 'tinyint'.但是,在数据库表中,CtryId 是一个“tinyint”。

I tried to set the type using我尝试使用设置类型

modelBuilder.Entity<Event>().Property(e => e.CountryId).HasColumnName("CtryId").HasColumnType("tinyint");

in the OnModelCreating method but get the following error:在 OnModelCreating 方法中,但收到以下错误:

error 2019: Member Mapping specified is not valid. The type 'Edm.Int32[Nullable=False,DefaultValue=]' of member 'CountryId' in type 'RA.Data.Event' is not compatible with 'SqlServer.tinyint[Nullable=False,DefaultValue=]' of member 'CtryId' in type 'CodeFirstDatabaseSchema.Event'.

How do I map a C# int to a SqlServer tinyint using Entity Framework Code First?如何使用实体框架代码优先将 C# int 映射到 SqlServer tinyint?

Short answer : You can't.简短的回答:你不能。

The mappings "line up" like below.映射“排列”如下。

The property on the POCO should be "byte". POCO 上的属性应该是“字节”。

    public byte CountryId{ get; set; }

and the Mapping:和映射:

        this.Property(t => t.CountryId).HasColumnName("CtryId");

You gotta play by the rules of EF.你必须遵守 EF 的规则。

However, the good news is that you can make it work with a little magic.然而,好消息是你可以用一点魔法让它工作。

Since you don't want to break the contract..... you can do a workaround.既然你不想破坏合同......你可以做一个解决方法。

public byte JustForMappingCtryId{ get; set; }

[NotMapped]
public int CountryId
{ 
    get
    { 
        return Convert.ToInt32(this.JustForMappingCtryId);
    } 
    set
    {
        if(value > 8 || value < 0 )
        {
              throw new ArgumentOutOfRangeException("Must be 8 or less, and greater or equal to zero.");
        }
        //this.JustForMappingCtryId = value;  /* Uncomment this code.. to put back the setter... you'll have to do the conversion here (from the input int to the byte) of course..but the edited out code shows the intention */      
    }
}

and the mapping:和映射:

   this.Property(t => t.JustForMappingCtryId).HasColumnName("CtryId");

And put an entity framework "ignore" attribute on CountryId.并在 CountryId 上放置一个实体框架“忽略”属性。 (above seen as [NotMapped]) .. OR use the Fluent way to specify the ignore (not shown in the code above) but below is a breadcrumb: (以上视为 [NotMapped]) .. 或使用 Fluent 方式指定忽略(上面的代码中未显示)但下面是面包屑:

 modelBuilder.Entity<MyEntity>().Ignore(c => c.MyPocoProperty);

There ya go.有你去。 It is a workaround, but should fit your need.这是一种解决方法,但应该适合您的需要。

In EF, int32 maps to int in the db.在 EF 中, int32映射到 db 中的int In this case, tinyint maps to byte in the .NET framework.在这种情况下, tinyint映射到 .NET 框架中的byte You should change your CountryId to the type of byte in the POCO model.您应该将CountryId更改为 POCO 模型中的byte类型。

You could create a new POCO class with byte column and use it instead of the old POCO class.您可以创建一个带有byte列的新 POCO 类并使用它代替旧的 POCO 类。

You could use AutoMapper to copy the values between old POCO class used in higher layers and new POCO class used in your repository layer for storing purposes.您可以使用 AutoMapper 在更高层中使用的旧 POCO 类和存储库层中使用的新 POCO 类之间复制值以进行存储。

暂无
暂无

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

相关问题 如何使用EntityFramework代码先将tinyint映射到C#字节 - How to map tinyint to c# byte using entityframework code first migration 如何使用实体框架代码优先将 Boolean 数组绑定到 C# model? - How do I bind a Boolean array to a C# model using Entity Framework Code First? 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first? 如何使用C#-WPF-Entity-Framework Code First应用程序创建数据库备份? - How do I create database backups using C#-WPF-Entity-Framework Code First application? 如何将tinyint映射到int? - How to map tinyint to int? 如何在MVC 4中使用Entity Framework从C#代码部分使用SQL函数 - How do I use a SQL function from C# code section using Entity Framework in MVC 4 C# 实体框架代码优先 - 如何仅使用该外键的 id 添加带有外键的行? - C# Entity Framework Code-First- How do you add a row with foreign key using just the id of that foreign key? c#实体框架5代码首先将三部分表名映射到实体 - c# entity framework 5 code first map three part table name to entity c# - 如何通过实体框架代码的流畅API首先在c#中更改表名? - how can i change table name by fluent API of entity framework code first in c#? 如何使用Code First Entity Framework在C#中包括可选关系 - How to include optional relations in C# using Code First Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM