RIA/EF4 实体属性映射到 NOT NULL nvarchar - 空字符串
背景:
- 实体框架 4
- Silverlight 4
- RIA 服务
- MSSQL Server 2008
我有一个实体,它具有名为“描述”的字符串属性。
在数据库中,它映射到 NOT NULL NVARCHAR(200)
。
问题:
当我尝试插入该实体的新行时,这就是我所做的:
MyExampleEntity entity = new MyExampleEntity()
{
Name = "example",
Description = "" // NOTE THIS LINE!
};
DatabaseContext db = new DatabaseContext();
db.MyExampleEntities.Add(entity);
db.SubmitChanges();
但是,这会导致异常,提示“描述字段是必需的。”
问题:
“空字符串”不应该只是 - 零个字符的字符串吗?
我认为只有 Description = null
才应被视为提供无价值。
- 为什么我的字符串有一个值(尽管其长度为 0),却被认为好像我省略了该值?
- 这种转变发生在什么水平上?在 RIA 上、在 EF 上还是在 MSSQL 上?
- 当我将
Description 设置为“”
时,有没有办法使描述具有零长度值,并在Description = null
(没有值)时引发异常?
Background:
- Entity Framework 4
- Silverlight 4
- RIA services
- MSSQL Server 2008
I have an entity that has a String property named Description.
In database it maps to the NOT NULL NVARCHAR(200)
.
Problem:
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."
Question:
Should not the "empty string" be simply that - a string with zero characters?
I believe only Description = null
should be treated as providing no value.
- 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 whenDescription = null
(having no value)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是实体框架的症状。
相关文章
一些数据注释可以用来克服这个问题:
This appears to be a symptom of Entity Framework.
Related Article
Some data annotations can be used to overcome this: