SQL Server 默认值不作为实体默认值出现
SQL Server 表列的默认值不会出现在实体中。
这是正常行为吗?
我试图弄清楚但没有结果。 感谢您的任何帮助。
Default value from SQL Server table's column does not appear in entity.
Is it normal behavior?
I have tried to figure it out but with no result.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实体在设计器中设置有自己的默认值。右键单击您的实体并选择属性以查看默认值。
Entities have their own default values set in the designer. Right click your entity and select properties to see the default value.
您可以通过在 edmx 文件的 SSDL 中编辑该属性来告诉实体框架数据库将处理该属性。
最初
我们已将其更改为< /b>
通过设置storeGeneratePattern="Compulated" 我们可以告诉 EF 该属性值将由 DB 插入。
对于编辑 SSDL
2.Ctrl+F 属性名称并更改该属性
you can tell entity framework that database will take care of that property by editing that property in SSDL of the edmx file.
Initially
<Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" />
we have change it to
<Property Name="CompanyName" Type="nvarchar" Nullable="false" MaxLength="40" StoreGeneratedPattern="Computed" />
by setting storeGeneratedPattern="Computed" we can tell to EF that the property value will be inserted by DB.
For Editing SSDL
2.Ctrl+F name of the property and just change that property
实体框架为每个属性提供显式值。在 SQL 中,您可以编写一个查询,将值插入到
[Field1]
中,并将[Field2]
和[Field3]
留空,它们将使用数据库默认值。但是由于实体框架根据实体上定义的属性生成查询,因此所有列将始终被赋予一个值,因此如果您想提供默认值,则需要在设计器中设置,我不认为创建实体模型时会自动拾取它。Entity Framework provides an explicit value for each property. In SQL you can write a query that inserts a value into
[Field1]
and leave[Field2]
and[Field3]
blank, and they will use the database default. But since Entity Framework generates the query based on the properties defined on the entity, all columns will always be given a value, so if you want to provide a default value, it will need to be set in the designer, I don't think it gets automatically picked up when the entity model is created.