2014-06-10 86 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會照顧創建你的數據庫,從而刪除數據庫和它留給初始化創建結構和種子數據。

順便說一下,如果你想支持生產代碼,你應該考慮遷移而不是數據庫初始化程序。