EFCode第一个属性为空的问题 [英] EFCode First Property Null problem

查看:107
本文介绍了EFCode第一个属性为空的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用EFCode先在asp.net MVC 3的模式。 (实体框架4.0和EFCode首先0.8)
中的模型定义象下面这样:

I use EFCode First in asp.net mvc 3 model. (Entity Framework 4.0 and EFCode First 0.8) The model is defined like below:

public class User
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int WorkedYears { get; set; }
}

在使用 db.Users.Find(1 ),将抛出这个错误:

when use db.Users.Find(1), will throw this error:

在用户
中的'WorkedYears'属性可能不被设置为'空'的值。
必须设置该属性类型的Int32的
非空值

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

请注意: user.Id = 1存在于数据库中,并且该记录的WorkedYears为NULL。
如果我设置在数据库中WorkedYears = 0时,错误将消失,
并且如果我定义像属性:公众诠释? WorkedYears {搞定;设置;} ,错误会即消失了。
,但我不希望使用INT?和也想列保持为NULL,如果未设定。
那么,有没有其他的解决办法来解决呢?

Note: user.Id=1 exists in database, and WorkedYears of the record is NULL. if I set the WorkedYears = 0 in the database, the error will disappear, and also if I define the property like:public int? WorkedYears{get; set;}, the error will disapper too. but I don't want use int?, and also want the column keep NULL if not set. So is there any other solution to fix it?

非常感谢。

推荐答案

您的模型不能反映你的数据库,因为它不工作。您必须定义 WorkedYears INT?。如果您认为有此属性强制你必须定义新的类视图模型,其中WorkedYears将是无效和处理又该该属性包含如果数据库包含空值。

Your model does not reflect your database and because of that it doesn't work. You must define WorkedYears with int?. If your view have this property mandatory you must define new class "view model" where WorkedYears will be null and handle what should that property contains if your database contains null.

正确POCO为EF:

public class User
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int? WorkedYears { get; set; }
}



正确的视图模型:

Correct view model:

public class UserModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int WorkedYears { get; set; }
}

您控制器负责将用户< - > 的usermodel 。如果你仍然想使用单类,你必须添加第二个没有对应的属性,这将在内部使用nullabe WorkedYears:

Your controller is responsible for converting User <-> UserModel. If you still want to use single class you must add second not mapped property which will internally use nullabe WorkedYears:

public class User
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int? WorkedYears { get; set; }
    [NotMapped]
    public int WorkedYearsDisplay
    {
        get
        {
            return WorkedYears ?? 0;
        }
        set
        {
            WorkedYears = value == 0 ? new Nullable<int>() : value;
        }
    }
}



结论:如果你的数据库中包含它可以包含空值可为空字段,您必须在您的映射类为空的属性。没有别的办法。

Conclusion: If your database contains nullable field which can contain null value you must have nullable property in your mapped class. No other way.

这篇关于EFCode第一个属性为空的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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