EF4实体属性映射到NOT

EF4实体属性映射到NOT

本文介绍了RIA / EF4实体属性映射到NOT NULL nvarchar - 空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景


  • 实体框架4

  • Silverlight 4

  • RIA服务

  • MSSQL Server 2008

  • Entity Framework 4
  • Silverlight 4
  • RIA services
  • MSSQL Server 2008

我有一个具有名为Description的String属性的实体。

I have an entity that has a String property named Description.

在数据库中,它映射到 NOT NULL NVARCHAR(200)

In database it maps to the NOT NULL NVARCHAR(200).

问题:

当我尝试插入该实体的新行时,这就是我所做的: p>

When I try to insert a new row of that entity, this is what I do:

MyExampleEntity entity = new MyExampleEntity()
{
    Name = "example",
    Description = ""        // NOTE THIS LINE!
};

DatabaseContext db = new DatabaseContext();
db.MyExampleEntities.Add(entity);
db.SubmitChanges();

但是,这会导致一个例外,说描述字段是必需的。

This, however, causes an exception saying "The Description field is required."

问题:

空字符串只是 - 一个字符串,零字符

Should not the "empty string" be simply that - a string with zero characters?

我认为只有 描述= null 应被视为提供

I believe only Description = null should be treated as providing no value.


  • 为什么我的字符串有一个值(虽然它的长度是0),被认为是我省略了这个值?

  • 这个转换在什么级别发生?在RIA上,在EF或MSSQL?

  • 当我将描述设置为并且在 Description = null (没有值)时引起异常?

  • Why is my string, which has a value (although its length is 0), considered to be as if I have omitted the value?
  • On what level does this conversion happen? On RIA, on EF or in MSSQL?
  • Is there a way to make a description have zero-length value when I set the Description to "" and cause an exception when Description = null (having no value)?

推荐答案

这似乎是Entity Framework的一个症状。

This appears to be a symptom of Entity Framework.

可以使用一些数据注释来克服这个原因:

Some data annotations can be used to overcome this:

[MetadataType(typeof(Report_META))]
   public partial class Report
   {
   }

   public partial class Report_META
   {
       [Required(AllowEmptyStrings = true)]
       [DisplayFormat(ConvertEmptyStringToNull = false)]
       public object Note { get; set; }
    }

这篇关于RIA / EF4实体属性映射到NOT NULL nvarchar - 空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 03:35