为什么“DatabaseGeneratedOption.None”存在? [英] Why does the `DatabaseGeneratedOption.None` exist?

查看:797
本文介绍了为什么“DatabaseGeneratedOption.None”存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与 Property(()=> p).HasDatabaseGeneratedOption()调用一起使用。是否可能关闭默认的DB值生成?

解决方案

EF使用 DatabaseGeneratedOption 确定如何处理新实体的键列的值。如果 DatabaseGeneratedOption Identity EF知道属性设置的值可以被忽略,应该从数据库中使用。如果 DatabaseGeneratedOption EF将把属性的值作为键列的值插入数据库。在代码首先 - 当Code First约定找到一个int属性,该属性可以是给定实体的关键属性时,它们默认将这个列配置为标识列(意思是数据库将生成键列/属性的值)。 DatabaseGeneratedOption.None 允许你覆盖这个,如果你想自己设置键值。


This is used with the Property(() => p).HasDatabaseGeneratedOption() call. Is is perhaps to turn off default DB value generation?

解决方案

EF uses DatabaseGeneratedOption to figure out what to do with the value of a key column for new entities. If the DatabaseGeneratedOption is Identity EF knows that the value the property is set to can be ignored and that the one that comes from the database should be used. If the DatabaseGeneratedOption is None EF will insert the value of the property to the database as the value of the key column.

In Code First - when Code First conventions find an int property that can be the key property for the given entity they by default will configure this column as identity column (meaning the database will generate the value of the key column/property). DatabaseGeneratedOption.None allows you to overwrite this if you want to set key values on your own.

这篇关于为什么“DatabaseGeneratedOption.None”存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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