简体   繁体   English

尝试向 edmx 文件添加新视图不会创建

[英]Trying to add new view to edmx file will not create

I created an ADO.NET Entity Data Model and now I want to add a new view to it.我创建了一个 ADO.NET 实体数据模型,现在我想向它添加一个新视图。 I found this source and went through the steps but the new view is not adding to the Model.我找到了这个来源并完成了这些步骤,但新视图没有添加到模型中。 Here is the steps I followed:这是我遵循的步骤:

1) Right Clicked on my .edmx file and chose Open With 1) 右键单击​​我的 .edmx 文件并选择打开方式
2) Chose ADO.NET Entity Data Model Designer 2)选择ADO.NET实体数据模型设计器
3) Right Clicked in the Data Model Designer (in the Add tab) and chose Update Model From Database 3) 右键单击​​数据模型设计器(在添加选项卡中)并选择从数据库更新模型
4) Checked off my new view and hit finish 4)检查我的新视图并点击完成
5) Saved the Model Designed and said okay to the scripts running 5) 保存设计好的模型并对运行的脚本说好的

After Which my new view is not in the Model Designer or is there a new class in my solution explorer.之后我的新视图不在模型设计器中,或者我的解决方案资源管理器中有一个新类。 What am I missing?我错过了什么? What else would someone need to see to help me diagnose this?其他人还需要看什么来帮助我诊断这个问题?

Entity Framework DB first will often ignore your view if you don't have a primary key defined.如果您没有定义主键,Entity Framework DB 首先通常会忽略您的视图。

If you wish to make it work, in DATABASE, use the ISNULL() function and/or unique constraint on one of your field.如果您希望使其工作,请在 DATABASE 中对您的字段之一使用ISNULL()函数和/或唯一约束。 Make sure this field is unique.确保该字段是唯一的。

Refer to this question if needed :如果需要,请参阅此问题:

Views,Entity,Cannot deduce a primary key 视图、实体、无法推导出主键

For a combination of 2 fields being unique, one simple but not very clean way is to add another column in your view, containing a concatenation of the two fields and put them in isnull().对于唯一的 2 个字段的组合,一种简单但不是很干净的方法是在您的视图中添加另一列,包含两个字段的串联并将它们放在 isnull() 中。 Suppose it's two nvarchar(5) :假设它是两个 nvarchar(5) :

select isnull(columnA + columnB, '1234567890'), columnA, columnB, ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM