实体框架从视图保存加载到表

时间:2018-03-01 16:23:16

标签: c# entity-framework-6 repository-pattern

实体框架6 (代码首先来自db)我有一些实体,我从sql视图中获取。该视图包含一些我需要能够显示的计算字段, 但我无法保存回视图,因为它做了几个连接和其他不会让我保存的东西。

有没有什么方法可以从视图中读取,但是保存回表格(表格与视图相同,减去计算字段)?

我知道这在细节上很少,但我基本上想知道这是否可能,或者我是否应该采用不同的方式。

感谢

    [Table("SDWorkshop.vWorkshop")]
    public partial class Workshop
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Workshop()
        {
            Registered = new HashSet<Registration>();
            Waitlist = new HashSet<Waitlist>();
        }

        [Key]
        public int Workshop_ID { get; set; }

        //removed a bunch of fields for berevity        

        public int? Capacity { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public int? NumberRegistered { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public int? ActualNumberRegistered { get; set; }

        [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
        public int? EmptySeats { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public ICollection<Registration> Registered { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public ICollection<Waitlist> Waitlist { get; set; }
}

caluclated字段是标记为数据库生成的字段,该部分用于保存回视图,但sql生成错误,因为视图使用连接来填充这些计算字段。

3 个答案:

答案 0 :(得分:1)

您可以将更新选项配置为使用存储过程,您可以在其中修改所需的表 Here the docs for do it

答案 1 :(得分:1)

感谢您的回答,我想我也可以使用存储过程,但最终让我的DBA为插入创建了一个INSTEAD OF触发器。

这样的事情:

CREATE TRIGGER tr_vWorkshop_InsteadOfInsert ON [SDWorkshop].[vWorkshop]
INSTEAD OF INSERT
AS
BEGIN
    INSERT INTO SDWorkshop.Workshop
            ({columns here})

        SELECT      
             {columns here}

        FROM inserted
END

SELECT [Workshop_ID] FROM [SDWorkshop].[Workshop] 
WHERE @@ROWCOUNT > 0 AND [Workshop_ID] = SCOPE_IDENTITY() 

需要底部选择才能返回新创建的行的ID,否则实体框架认为没有创建任何内容。

答案 2 :(得分:0)

我是这样做的:

主要型号

public class RequestModel
{
    public int Id { get; set; }
    public int RequestTypeId { get; set; }
    public int RequestStatusId { get; set; }
    public string Created { get; set; }
    [ForeignKey("FromUser")]
    public string FromUserId { get; set; }
    [ForeignKey("ToUser")]
    public string ToUserId { get; set; }
    public string RequestData { get; set; }
    public string RequestResult { get; set; }
    public bool RequestIsActive { get; set; }

    public ApplicationUser ToUser { get; set; }
    public ApplicationUser FromUser { get; set; }

}

视图模型

public class RequestView
    {
        public int Id { get; set; }
        public int RequestTypeId { get; set; }
        public string RequestTypeName { get; set; }
        public int RequestStatusId { get; set; }
        public string RequestStatusName { get; set; }
        public string Created { get; set; }

        public string FromUserId { get; set; }
        public string FromUserName { get; set; }

        public string ToUserId { get; set; }
        public string ToUserName { get; set; }
        public string RequestResult { get; set; }
        public bool RequestIsActive { get; set; }

    }

并在“实体配置”中:

  public class RequestConfiguration : EntityTypeConfiguration<RequestModel>
{
    public RequestConfiguration()
    {
        ToTable("QMS_REQUESTS").HasKey(p =>p.Id);


        Property(p => p.Id).HasColumnName("REQUEST_ID").IsRequired();
        Property(p => p.RequestTypeId).HasColumnName("REQUEST_TYPE_ID");
        Property(p => p.RequestData).HasColumnName("REQUEST_DATA").HasMaxLength(800);
        Property(p => p.RequestResult).HasColumnName("REQUEST_RESULT").HasMaxLength(150);
        Property(p => p.RequestStatusId).HasColumnName("REQUEST_STATUS_ID");
        Property(p => p.Created).HasColumnName("REQUEST_CREATED").HasMaxLength(50);
        Property(p => p.RequestIsActive).HasColumnName("REQUEST_IsActive");
        Property(p => p.FromUserId).HasColumnName("FROM_USER");
        Property(p => p.ToUserId).HasColumnName("TO_USER");
    }
}

public class RequestViewConfiguration : EntityTypeConfiguration<RequestView>
{
    public RequestViewConfiguration()
    {
        ToTable("VIEW_QMS_REQUESTS").HasKey(p => p.Id);


        Property(p => p.Id).HasColumnName("REQUEST_ID").IsRequired();
        Property(p => p.RequestTypeId).HasColumnName("REQUEST_TYPE_ID");
        Property(p => p.RequestTypeName).HasColumnName("REQUEST_TYPE_NAME");
        Property(p => p.RequestStatusId).HasColumnName("REQUEST_STATUS_ID");
        Property(p => p.RequestStatusName).HasColumnName("REQUEST_STATUS_NAME");
        Property(p => p.RequestResult).HasColumnName("REQUEST_RESULT").HasMaxLength(150);
        Property(p => p.Created).HasColumnName("REQUEST_CREATED").HasMaxLength(50);
        Property(p => p.RequestIsActive).HasColumnName("REQUEST_IsActive");
        Property(p => p.FromUserId).HasColumnName("FROM_USER");
        Property(p => p.FromUserName).HasColumnName("FROM_USERNAME");
        Property(p => p.ToUserId).HasColumnName("TO_USER");
        Property(p => p.ToUserName).HasColumnName("TO_USERNAME");
    }
}

在迁移中:

  public partial class RequestView : DbMigration
{
    public override void Up()
    {

                Sql(@"
                CREATE VIEW [dbo].[VIEW_QMS_REQUESTS]
                AS
                SELECT        dbo.QMS_REQUESTS.REQUEST_ID, dbo.VIEW_QMS_COM_REQUEST_TYPES.REQUEST_TYPE_ID, dbo.VIEW_QMS_COM_REQUEST_TYPES.REQUEST_TYPE_NAME_AR AS REQUEST_TYPE_NAME, 
                        dbo.VIEW_QMS_REQUEST_STATUS_TYPES.REQUEST_STATUS_ID, dbo.VIEW_QMS_REQUEST_STATUS_TYPES.REQUEST_STATUS_NAME_AR AS REQUEST_STATUS_NAME, dbo.QMS_REQUESTS.REQUEST_CREATED, 
                        dbo.QMS_REQUESTS.FROM_USER, dbo.AspNetUsers.UserName AS FROM_USERNAME, dbo.QMS_REQUESTS.TO_USER, AspNetUsers_1.UserName AS TO_USERNAME, dbo.QMS_REQUESTS.REQUEST_RESULT, 
                        dbo.QMS_REQUESTS.REQUEST_IsActive
                FROM            dbo.QMS_REQUESTS INNER JOIN
                        dbo.VIEW_QMS_COM_REQUEST_TYPES ON dbo.QMS_REQUESTS.REQUEST_TYPE_ID = dbo.VIEW_QMS_COM_REQUEST_TYPES.REQUEST_TYPE_ID INNER JOIN
                        dbo.VIEW_QMS_REQUEST_STATUS_TYPES ON dbo.QMS_REQUESTS.REQUEST_STATUS_ID = dbo.VIEW_QMS_REQUEST_STATUS_TYPES.REQUEST_STATUS_ID INNER JOIN
                        dbo.AspNetUsers ON dbo.QMS_REQUESTS.FROM_USER = dbo.AspNetUsers.Id INNER JOIN
                        dbo.AspNetUsers AS AspNetUsers_1 ON dbo.QMS_REQUESTS.TO_USER = AspNetUsers_1.Id
                GO
                ");
    }

    public override void Down()
    {
        Sql(@"DROP VIEW [dbo].[VIEW_QMS_REQUESTS] GO");
    }

在ApplicationDbContext中:

  public virtual DbSet<RequestModel> Requests { get; set; }
  public virtual DbSet<RequestView> RequestsView { get; set; }

我希望你明白。