更新表,拆分实体时列名无效

时间:2013-10-14 17:28:37

标签: mapping entity-framework-5

当我尝试使用SaveChanges()插入新实体时,我得到以下异常。

System.Data.Entity.Infrastructure.DbUpdateException : An error occurred while updating the entries. See the inner exception for details.
  ----> System.Data.UpdateException : An error occurred while updating the entries. See     the inner exception for details.
  ----> System.Data.SqlClient.SqlException : Invalid column name 'LD_Content'.
Invalid column name 'LD_File'.

我这里没有使用Code First或Database First方法。数据库已存在,但实体代码是从自定义模板生成的。

这些是数据库表:

CREATE TABLE [dbo].[Licences](
    [L_ID] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_Licences] PRIMARY KEY NONCLUSTERED 
(
    [L_ID] ASC
))
GO

ALTER TABLE [dbo].[Licences]  WITH CHECK ADD  CONSTRAINT [FK_Licences_LicenceData] FOREIGN KEY([L_ID])
REFERENCES [dbo].[LicenceData] ([LD_ID])
GO

CREATE TABLE [dbo].[LicenceData](
    [LD_ID] [uniqueidentifier] NOT NULL,
    [LD_Content] [varbinary](max) NOT NULL,
    [LD_File] [varbinary](max) NULL,
 CONSTRAINT [PK_LicenceData] PRIMARY KEY NONCLUSTERED 
(
    [LD_ID] ASC
))

这些是实体:

[Table("Licences")]
public class Licence
{
    [Required, Display(Name="ID"), Column("L_ID")]
    public Guid ID { get; set; }

    public virtual LicenceFile File { get; set; }

    [Required, Display(Name = "Content"), Column("LD_Content")]
    public virtual string Content { get; set; }
}


[Table("LicenceData")]
public class LicenceFile
{
    [Required, Display(Name = "ID"), Column("LD_ID")]
    public Guid ID { get; set; }

    [Display(Name = "File"), Column("LD_File")]
    public byte[] File { get; set; }
}

这是映射:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Licence>()
    .Map(m =>
    {
    m.Properties(licence => new {licence.Content});
    m.ToTable("LicenceData");
    });

    modelBuilder.Entity<Licence>().HasRequired(i => i.File).WithRequiredPrincipal();

    base.OnModelCreating(modelBuilder);
}

创建实体时,其导航属性设置为:

 new Licence
 {
    Content = "content",
    File = new LicenceFile()
 }

总结一下,有一对一关系的两个表。其中一个实体,许可证,映射到其表(许可证),也映射到另一个表的一个列(实体拆分)。第二个实体LicenceFile映射到另一个表(LienceData)的ramaining列。

这里出了什么问题?

0 个答案:

没有答案