如何第一映射实体框架代码列不同数据类型的实体属性格式 [英] How to map column and entity propery of different datatypes in entity framework code first

查看:115
本文介绍了如何第一映射实体框架代码列不同数据类型的实体属性格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架。5 - 代码第一个



我有我连接到一个已经存在有一段时间了一个数据库(我没有创建它)。有一个名为 T_Customers表。它包含了所有客户的名单。它具有以下结构(仅部分示出):



  CUSTOMER_ID |数字(5,0)|自动递增| NOT NULL(未设置主键)
FName参数| VARCHAR(50)|不为空
LName的| VARCHAR(50)|不为空



我的客户类:

 公共类客户:IEntity 
{
公众诠释标识{搞定;组; }

公共字符串名字{获得;组; }

公共字符串名字{获得;组; }
}



我的 IEntity 接口:

 公共接口IEntity 
{
INT标识{搞定;组; }
}



我在我的数据库上下文类以下内容:

 公共类DatabaseContext:的DbContext 
{
公共DatabaseContext(字符串的connectionString)
:底座(的connectionString)
{
}

公共DbSet<客户>客户{搞定;组; }

保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
{
modelBuilder.Configurations.Add(新CustomerConfiguration());
}

公开新DbSet< TEntity> SET< TEntity>()
其中TEntity:类,IEntity
{
返回base.Set< TEntity>();
}
}



我的客户配置类:

 类CustomerConfiguration:EntityTypeConfiguration<客户> 
{
内部CustomerConfiguration()
{
this.ToTable(T_Customers);
this.Property(X => x.Id).HasColumnName(CUSTOMER_ID);
this.Property(X => x.FirstName).HasColumnName(FName参数);
this.Property(X => x.LastName).HasColumnName(LName的);
}
}



我想在我的实体声明一致我需要所有的ID是整数。此客户ID是在数据库类型的数字,并试图返回所有的客户列表时,现在我遇到的问题。我如何从数据库数值类型到C#整数类型映射?我不会改变我的课的ID可空或小数,我的ID始终不为空的和整数。我也无法更改数据库



这是我收到的错误是:



 在客户的id属性不能被设置为十进制的价值。 
必须将此属性设置为类型的非空值的Int32。


解决方案

指定数值类型列

 属性(X => x.Id).HasColumnName(CUSTOMER_ID)HasColumnType(数字)。 

在生成的数据库,它会创建一个精确的数字列 18,0 。但是,当你映射到现有的数据库,它会正常工作与 5,0 数字列。


I am using Entity Framework 5 - Code first.

I have a database that I am connecting to that already exists for some time now (I did not create it). There is a table called T_Customers. It contains a list of all the customers. It has the following structure (only partially shown):

Customer_id | numeric (5, 0) | auto increment | not null (not set as primary key)
FName | varchar(50) | not null
LName | varchar(50) | not null

My Customer class:

public class Customer : IEntity
{
     public int Id { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }
}

My IEntity interface:

public interface IEntity
{
     int Id { get; set; }
}

I have the following in my database context class:

public class DatabaseContext : DbContext
{
     public DatabaseContext(string connectionString)
          : base(connectionString)
     {
     }

     public DbSet<Customer> Customers { get; set; }

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
          modelBuilder.Configurations.Add(new CustomerConfiguration());
     }

     public new DbSet<TEntity> Set<TEntity>()
          where TEntity : class, IEntity
     {
          return base.Set<TEntity>();
     }
}

My customer configuration class:

class CustomerConfiguration : EntityTypeConfiguration<Customer>
{
     internal CustomerConfiguration()
     {
          this.ToTable("T_Customers");
          this.Property(x => x.Id).HasColumnName("Customer_id");
          this.Property(x => x.FirstName).HasColumnName("FName");
          this.Property(x => x.LastName).HasColumnName("LName");
     }
}

I'm trying to be consistent in my entity declarations, I need all the IDs to be integers. This customer ID is of type numeric in the database, and now I am running into issues when trying to return a list of all the customers. How do I map from a database numeric type to a C# integer type? I'm not changing my class's ID to nullable or decimal, my IDs are always non nullable and integer. I also cannot change the database.

The error that I am getting is:

The 'Id' property on 'Customer' could not be set to a 'Decimal' value. 
You must set this property to a non-null value of type 'Int32'.

解决方案

Specify numeric type for column

Property(x => x.Id).HasColumnName("Customer_id").HasColumnType("numeric");

When generating database, it will create numeric column with precision 18,0. But when you are mapping to existing database, it will work fine with 5,0 numeric column.

这篇关于如何第一映射实体框架代码列不同数据类型的实体属性格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆