EF Code First Readonly列 [英] EF Code First Readonly column

查看:210
本文介绍了EF Code First Readonly列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF代码,首先采用数据库第一种方法。
with Database.SetInitializer(null);



我的表有两列createddate和modifyddate。它们由SQL Server使用触发器进行管理。这个想法是,当数据输入发生时,这些列通过触发器获取数据。



现在我想做的是使这个只读从EF代码第一观点。即我想要看到我的应用程序中创建的日期和日期,但我不想修改这些数据。



我已经尝试在setter上使用私有修饰符,但没有运气。当我尝试向表中添加新数据时,它尝试将DateTime.Max日期输入到数据库,



任何想法?

解决方案

你不能使用私有修饰符,因为EF本身需要在加载实体时设置属性,而Code First只能在属性具有公共设置器时执行此操作(与EDMX相反,私有设置器可能(1)(2))。



您需要做的是标记您的 CreatedDate DatabaseGeneratedOption.Identity 和您的 AmendDate DatabaseGeneratedOption.Computed 。这将允许EF从数据库正确加载数据,在插入或更新后重新加载数据,以便实体在应用程序中是最新的,并且同时不允许您更改应用程序中的值,因为该值设置在该应用程序将永远不会传递到数据库。从面向对象的角度来看,它不是一个非常好的解决方案,但从功能的角度来看,它正是你想要的。



你可以使用数据注释: p>

  [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
public DateTime CreatedDate {get;组; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime AmendDate {get;组;

或使用流畅的API在 OnModelCreating 覆盖在您的派生上下文中:

  modelBuilder.Entity< YourEntity>()
.Property(e => CreatedDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity< YourEntity>()
.Property(e => e.AmendDate)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);


I am using EF Code first with database first approach. "with Database.SetInitializer(null);"

My table has two columns createddate and amendddate. They are managed by SQL Server using triggers. The idea is that when data entry happens then these columns gets data via triggers.

Now What I want to do is to make this read only from EF Code first point of view. I.e. I want to be able to see the createddate and ameneded dates from my app but I dont want to amend these data.

I have tried using private modifiers on setter but no luck.When I try to add new data to the table it tried to enter DateTime.Max date to the database which throws error from SQL server.

Any idea?

解决方案

You cannot use private modifiers because EF itself needs to set your properties when it is loading your entity and Code First can only do this when a property has public setter (in contrast to EDMX where private setters are possible (1), (2)).

What you need to do is mark your for CreatedDate with DatabaseGeneratedOption.Identity and your AmendDate with DatabaseGeneratedOption.Computed. That will allow EF to correctly load data from the database, reload data after insert or update so that entity is up to date in your application and at the same time it will not allow you to change the value in the application because the value set in the application will never be passed to the database. From an object oriented perspective it is not a very nice solution but from the functionality perspective it is exactly what you want.

You can do it either with data annotations:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public DateTime CreatedDate { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime AmendDate { get; set; }

Or with fluent API in OnModelCreating override in your derived context:

modelBuilder.Entity<YourEntity>() 
            .Property(e => e.CreatedDate)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<YourEntity>()
            .Property(e => e.AmendDate)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

这篇关于EF Code First Readonly列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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