简体   繁体   English

从实体框架模型生成数据库

[英]Generating database from entity framework model

I'm planning on making an entity framework class that will contain several models, Those models will create the database table structure. 我正计划制作一个包含多个模型的实体框架类,这些模型将创建数据库表结构。

Did some work and came up with this model structure: 做了一些工作,并提出了这个模型结构:

public class Search
{
    public int SearchId { get; set; }
    public string SearchName { get; set; }
    public Times t { get; set; }
    public Media m { get; set; }

}


public class Times
{
    public int TimesId { get; set; }
    DateTime FromDate { get; set; }
    DateTime ToDate { get; set; }
    String Days { get; set; }
    List<HourSlice> TSlist;
}

public class HourSlice
{
    string Name { get; set; }
    string Slice { get; set; } // will contain both -from hour- and -to hour- slice

}



public class Media
{
    int MediaId { get; set; }
    List<OperatorType> ot;
    List<Operator> op;
    List<Program> p;
    List<Sector> s;
    List<Categorie> cat;
    List<MisradPirsum> mp;
}

public class OperatorType
{
    int OperatorTypeID { get; set; }
    string OperatorTypeName { get; set; }
}

public class Operator
{
    int OperatorID { get; set; }
    string OperatorName { get; set; }

}

public class Program
{
    int ProgramID { get; set; }
    string ProgramName { get; set; }
}

public class Sector
{
    int SectorID { get; set; }
    string SectorName { get; set; }
}

public class Categorie
{
    public int CategorieId { get; set; }
    List<Branch> br;
    List<SubBranch> sbr;
    List<SubSubBranch> ssbr;

}

public class Branch
{
    int BranchID { get; set; }
    string BranchName { get; set; }
}

public class SubBranch
{
    int SubBranchID { get; set; }
    string SubBranchName { get; set; }

}

public class SubSubBranch
{
    int SubSubBranchID { get; set; }
    string SubSubBranchName { get; set; }
}

public class MisradPirsum
{
    public int MisradPirsumId { get; set; }
    List<Company> c;
}

public class Company
{
    int CompanyID { get; set; }
    string CompanyName { get; set; }
}

The initiator on Global.asax: Global.asax上的启动器:

    public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        Database.DefaultConnectionFactory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["DBS"].ConnectionString);
        Database.SetInitializer<DBS>(new Search_Initializer());

        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);

     // Database.SetInitializer<PlayWithMVC4.Models.DBS>(new DropCreateDatabaseAlways<PlayWithMVC4.Models.DBS>());
        Database.SetInitializer<PlayWithMVC4.Models.DBS>(new DropCreateDatabaseIfModelChanges<PlayWithMVC4.Models.DBS>());

    }

The Search class is the parent of all other classes as you can see. 如您所见,Search类是所有其他类的父类。 Underneath it the Times table that will relate to the Hourslice table. 在其下的Times表将与Hourslice表相关。 The Media table is also underneath the Search class and it has 6 tables underneath it, Several has their own child tables, each parent has it's own ID. Media表也位于Search类的下面,它下面有6个表,几个有自己的子表,每个父表都有自己的ID。

The main goal is to create a search database that will have all possible data structure to save searches and interact with the original data-database to get the actual data of each search properties/parameters. 主要目标是创建一个搜索数据库,该数据库将具有所有可能的数据结构以保存搜索并与原始数据库进行交互以获得每个搜索属性/参数的实际数据。

I'm having problem creating the database with this error: "error 3004: Problem in mapping fragments starting at line 30:No mapping specified for properties Search.m in Set Searches.\\r\\nAn Entity with Key (PK) will not round-trip when:\\r\\n Entity is type [PlayWithMVC4.Models.Search]\\r\\n"}" 我在使用此错误创建数据库时遇到问题:“错误3004:从第30行开始的映射片段中的问题:在Set Searches中没有为属性Search.m指定映射。\\ r \\ n带有键(PK)的实体将不会舍入-trip when:\\ r \\ n实体类型为[PlayWithMVC4.Models.Search] \\ r \\ n“}”

I'm sure something has done wrong with the structure/logic, But I'm not sure where to start digging, Help would be much appreciated. 我确定结构/逻辑出了点问题,但是我不确定从哪里开始挖掘,非常感谢帮助。

Firstly, you want to ensure that all of the properties that you are mapping are public and that they are actually properties and not fields. 首先,您要确保要映射的所有属性都是公共属性,并且它们实际上是属性而不是字段。

The members on your Media type for example are all private as you haven't specified a public access modifier. 例如,您的Media类型上的成员都是私有的,因为您尚未指定公共访问修饰符。 The collection members are also fields, not properties, as they have no getters and setters. 集合成员也是字段,而不是属性,因为它们没有getter和setter。

Also, if you wish to support proxy types for change tracking or lazy loading, then have a look here for the full list of requirements. 另外,如果您希望支持代理类型以进行更改跟踪或延迟加载,请在此处查看完整的要求列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM