错误2019:使用Entity Framework Code First指定的成员映射无效 [英] error 2019: Member Mapping specified is not valid using Entity Framework Code First

查看:317
本文介绍了错误2019:使用Entity Framework Code First指定的成员映射无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误详细信息:

(18,12):错误2019:指定的成员映射无效。类型'IMS.DAL.Users_mast'中成员'status'的类型'Edm.Byte [Nullable = False,DefaultValue =]'与'SqlServer.binary不兼容[Nullable = False,DefaultValue =,MaxLength = 8000,FixedLength ='True]''CodeFirstDatabaseSchema.Users_mast'类型中的'成员'状态'。



我的代码:

User_mast.cs文件包含:

[Column(TypeName = binary)]

public byte status {get;组; } //活动|非活动(1 | 0),我希望状态列在sql server中创建为二进制列。



context.cs文件包含:

公共类上下文:DbContext

{

公共上下文()

:base(name = sqlConn )

{

}



public DbSet< Users_mast>用户{get;组; }



protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

base.OnModelCreating(modelBuilder); < br $>
}

}



最后,

test.cs文件包含:



context obj = new context();



public void add ()

{

obj.Users.Add(new Users_mast(){user_fname =Agnib,user_lname =Pyne,user_email =a@gmail.com ,user_mobile =9830972301,username =agnib,pwd =As1232,created_date = DateTime.Now,status = 1});

obj.SaveChanges();

}



Error Details:
(18,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Byte[Nullable=False,DefaultValue=]' of member 'status' in type 'IMS.DAL.Users_mast' is not compatible with 'SqlServer.binary[Nullable=False,DefaultValue=,MaxLength=8000,FixedLength=True]' of member 'status' in type 'CodeFirstDatabaseSchema.Users_mast'.

My Code:
User_mast.cs file contains:
[Column(TypeName = binary)]
public byte status { get; set; } // Active | Inactive (1 | 0 ), where I want status column to be created as binary column in sql server.

context.cs file contains:
public class context : DbContext
{
public context()
: base("name=sqlConn")
{
}

public DbSet<Users_mast> Users { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}

and finally,
test.cs file contains:

context obj = new context();

public void add()
{
obj.Users.Add(new Users_mast() {user_fname="Agnib", user_lname="Pyne", user_email="a@gmail.com", user_mobile="9830972301", username="agnib", pwd="As1232", created_date=DateTime.Now, status=1 });
obj.SaveChanges();
}

On running, I am getting the above error. I can understand its due to the status field set as byte which is not getting mapped as binary.

Then what should I do to make the column in sql server as binary? Is there any list of mapping between sql server types and .net types specially in case of EF Code-First?

Any help is appreciated. Please help!!!

推荐答案

如果你只需要在列中存储0或1个值,你可以在sql中使用bit数据类型.net中的服务器和布尔数据类型。

也.net Byte []将在sql server中转换为二进制文件
if you only need to store 0 or 1 values in the column you can simply use bit datatype in sql server and Boolean data type in .net.
also .net Byte[] will convert as binary in sql server


这篇关于错误2019:使用Entity Framework Code First指定的成员映射无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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