2014-06-10 44 views
0

我厌倦了上传Excel表格并在MySQL数据库中插入数据。 当我调试我得到以下错误:模型兼容性无法检查,因为数据库不包含模型元数据。模型

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

下面我的代码:

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的entites

[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; } 
} 

优秀的entites

[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(); 
    } 

}

+0

我在下面教程http://www.c-sharpcorner.com/UploadFile/17e8f6/uploading-multiple-records-via-excel-upload-in-database-usin/ – stpdevi

+0

我的代码没有错?有没有人找到我犯的错误,请帮助我 – stpdevi

回答

0

这个错误暗示的是数据库不是由实体框架创建的,因此它不能确定你的模型是否可以用于这个数据库。究其原因最有可能是你已经创建了文章中的脚本的数据库:

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

然而ExcelStructureInitializer会照顾创建你的数据库,从而删除数据库和它留给初始化创建结构和种子数据。

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