【问题标题】:Model compatibility cannot be checked because the database does not contain model metadata. Model无法检查模型兼容性,因为数据库不包含模型元数据。模型
【发布时间】:2014-07-31 06:23:48
【问题描述】:

我很累上传 Excel 表并在 MySQL 数据库中插入数据。 当我调试时出现以下错误:

无法检查模型兼容性,因为数据库不包含模型元数据。只能检查使用 Code First 或 Code First 迁移创建的数据库的模型兼容性。

在我的代码下面:

enter code here
 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
                Database.SetInitializer<EFDbContext>(new UploadUsingEntity.Domain.Concrete.ExcelStructureInitializer());

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
        AuthConfig.RegisterAuth();
    }

帮助类

  public DataSet ReadExcelFile(string filePath, out string msg, string isHDR = "Yes")
    {
        string details = string.Empty;
        List<Excelstructure> lstEntityTable = repository.ExcelStructure.Where(
                     x => x.File_Name.Equals("EmployeeExcel", StringComparison.InvariantCultureIgnoreCase)).
            OrderBy(x => x.Row_No).ToList();
        List<string> lstFieldType = new List<string>();
        lstFieldType.AddRange(lstEntityTable[1].Field_Data.ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries));
        DataTable dt = CreateDataTableFromList(lstEntityTable);
        DataSet ds = GetDataFromMultipleSheets(filePath, lstFieldType);
        string fileName = string.Empty;
        for (byte i = 0; i < ds.Tables.Count; i++)
        {
            if (ds.Tables[i].Rows.Count > 0)
            {
                details = ValidateExcelHeadings(dt, ds.Tables[i]);
                ds.DataSetName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
            }
        }
        msg = details;
        return ds;
    }

ExcelStructure 实体

[Table("ExcelStructure")]
  public class Excelstructure
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Row_ID { get; set; }

    public int File_ID { get; set; }

    public string File_Name { get; set; }

    public string Field_Name { get; set; }

    public string Field_Data { get; set; }

    public int Active_Flag { get; set; }

    public int Row_No { get; set; }

    public string Field_Delimeter { get; set; }
}

杰出的实体

 [Table("outstanding")]
public class oustanding
{
   [Key]
    public string CustomerID { get; set; }
    public string Name { get; set; }
    public long PhoneNumber { get; set; }
    public string Address { get; set; }
    public int ServiceProviderID { get; set; }
    public string TotalDue { get; set; }
    public string LastPaidAmount { get; set; }
    public Nullable<System.DateTime> LastPaidDate { get; set; }
    public string OutStandingDescription { get; set; }
    public string longitudeandlatitude { get; set; }
}





 public class ExcelStructureInitializer : DropCreateDatabaseIfModelChanges<EFDbContext>
{
    protected override void Seed(EFDbContext context)
    {
        //base.Seed(context);
        var excelStructure = new List<Excelstructure>()
        {
            new Excelstructure(){ File_ID=1, Field_Name="outstanding", Field_Data="CustomerID|Name|PhoneNumber|Address|ServiceProviderID|TotalDue|LastPaidAmount|LastPaidDate|OutStandingDescription|longitudeandlatitude", Active_Flag=1, Row_No=1, File_Name="oustanding", Field_Delimeter="|"},
            new Excelstructure(){ File_ID=1, Field_Name="DataType", Field_Data="S|S|I|S|I|S|S|D|S|S", Active_Flag=1, Row_No=2, File_Name="outstanding", Field_Delimeter="|"},
            new Excelstructure(){ File_ID=1, Field_Name="Size", Field_Data="50|100|20|100|10|100|100|20|100|100", Active_Flag=1, Row_No=3, File_Name="outstanding", Field_Delimeter="|"},
            new Excelstructure(){ File_ID=1, Field_Name="Mandatory", Field_Data="Y|Y|Y|Y|Y|N|N|N|N|N", Active_Flag=1, Row_No=4, File_Name="outstanding", Field_Delimeter="|"},
            new Excelstructure(){ File_ID=1, Field_Name="Param", Field_Data="@CustomerID|@Name|@PhoneNumber|@Address|@ServiceProviderID|@TotalDue|@LastPaidAmount|@LastPaidDate|@OutStandingDescription|@longitudeandlatitude", Active_Flag=1, Row_No=5, File_Name="outstanding", Field_Delimeter="|"},
        };
        context.ExcelStructure.AddRange(excelStructure);
        context.SaveChanges();
    }

}

【问题讨论】:

标签: entity-framework-4 ef-code-first


【解决方案1】:

这个错误意味着数据库不是由实体框架创建的,所以它无法确定你的模型是否可以与这个数据库一起使用。原因很可能是您使用文章中的脚本创建了数据库:

CREATE TABLE [dbo].[ExcelStructure] ....

但是 ExcelStructureInitializer 将负责创建数据库,因此请删除数据库并将其留给初始化程序来创建结构和种子数据。

顺便说一下,如果你想支持生产代码,你应该考虑迁移而不是数据库初始化器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    相关资源
    最近更新 更多