应该是只读数据库字段的属性(Fluent NHibernate Automapping)
-
13-12-2019 - |
题
我将 Fluent NHibernate 与自动映射功能一起使用。现在我正在寻找诸如配置、设置、自定义属性之类的东西 - 无论什么 - 将实体属性声明为“ReadOnlyFromDb”
在 MsSql 数据库中,我在一个表中使用计算列,其中的值是根据特定数据行的其他一些值计算的。现在我已在实体类中将此列声明为
public virtual int STATUS { get; private set; }
获取表的具体数据后,一切正常。该物业 STATUS
使用数据表中的特定值正确填充。
当我尝试时出现问题 SaveUpdate()
具体对象。我总是遇到例外
A computed column cannot be the target of an INSERT or UPDATE statement
这对我的理解来说是正确的——而且它应该是这样的;)!
基本上我正在寻找配置、设置、自定义属性 - 无论如何 - 说
嘿 Fluent NHibernate - 获取特定属性
propertyName
但不要插入/更新属性propertyName
有这样的事吗?或者这种情况有解决方法吗?我搜索了 fluid nhibernate wiki 但没有找到类似的情况。
我希望有人已经面对并解决了这个问题!
这是我如何创建会话的代码片段(也许有帮助):
public static ISessionFactory GetNHibernateSession()
{
if (nhibernateSession != null)
return nhibernateSession;
if (ConfigurationManager.AppSettings[msSqlConnectionString] == null || ConfigurationManager.AppSettings[msSqlConnectionString] == String.Empty)
throw new NullReferenceException(String.Format("AppSetting '{0}' must not be null!", msSqlConnectionString));
//nhibernateSession = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager.AppSettings[msSqlConnectionString]))
// .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<T_ABWEICHUNGEN>().IgnoreBase<BaseClass>)).BuildSessionFactory();
nhibernateSession = Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.ConnectionString(ConfigurationManager.AppSettings[msSqlConnectionString]))
.Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(System.Reflection.Assembly.GetExecutingAssembly())
.Override<T_ABWEICHUNGEN>(map =>map.Map(d => d.A08_STATUS_FILE).Length(2147483647))
.IgnoreBase(typeof(BaseClass))
.IgnoreBase(typeof(IDColumn))
.IgnoreBase(typeof(MsSqlQuery))
.IgnoreBase(typeof(MsSqlParamCollection))
.IgnoreBase(typeof(AbweichungStatus))
)).BuildSessionFactory();
return nhibernateSession;
}
}
感谢到目前为止的回复 - 到目前为止对我有帮助。但是 - 有没有办法更“动态”地解决这个问题?
例如:
我想声明一个名为的自定义属性 [ReadOnlyDbField]
现在使用此 custom 属性声明实体的所有属性:只需读取该值,不要更新/插入它。
基本上我想说的是配置
Map all properties with the custom attribute [ReadOnlyDbField] to Not.Insert().Not.Update()
有办法实现吗?
解决方案
.Override<Entity>(map => map.Map(d => d.STATUS).Not.Insert().Not.Update())
更新:编辑问题的帐户
public class ReadonlyDbFielConvention : AttributePropertyConvention<ReadOnlyDbField>
{
protected override void Apply(ReadOnlyDbField attribute, IPropertyInstance instance)
{
instance.Not.Insert();
instance.Not.Update();
}
}
不隶属于 StackOverflow