简体   繁体   English

EF Code First关系法

[英]EF Code First approach on relationships

Being new to ASP .NET I've been researching on entity modeling approaches and it seems ORM using Entity Framework following Code First is the best approach for me. 作为ASP .NET的新手,我一直在研究实体建模方法,看来遵循Code First的使用Entity Framework的 ORM对我来说是最好的方法。

By following this tutorial, I have gotten the impression that, for each entity, you need to create a connectionstring (correct me if I'm wrong) and this fact confuses me when it comes to relational data, as, per this example, the database itself seems to cover just one entity. 通过阅读教程,我得到的印象是, 对于每个实体,您都需要创建一个connectionstring (如果我输入错误,请更正我),并且在涉及到关系数据时,这一事实使我感到困惑,例如,在此示例中,数据库本身似乎只覆盖一个实体。 So how is relational data handled in the EF. 那么在EF中如何处理关系数据。

PS: For unification purpose, please use entities Movies , Customers and the relational table named under the proper naming conventions. PS:出于统一目的,请使用实体MoviesCustomers和以正确命名约定命名的关系表。

You create a connection string per DbContext . 您每个DbContext创建一个连接字符串。 Here is the class that defines the DBContext: 这是定义DBContext的类:

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }
}

To add more tables to this context, add more lines like this (for examples, public DbSet<Customer> Customers { get; set; } . 要在此上下文中添加更多表,请添加更多这样的行(例如, public DbSet<Customer> Customers { get; set; }

public class MovieDBContext : DbContext
{
    public DbSet<Movie> Movies { get; set; }
    public DbSet<Ticket> Tickets { get; set; }
}

Accessing these from a context: 从上下文访问这些:

public class TicketController {
    private MovieDBContext db = new MovieDBContext ();

    public ActionResult Index(int movieId) {
       var listOfTickets = db.Tickets.Where(t=>t.MovieId == movieId).ToList();
       var parentMovie = db.Movie.Where(m=>m.Id == movieId).Single();
        ...
    }
}

Connection String: Contains initialization information that is passed as a parameter from a data provider to a data source. 连接字符串:包含初始化信息,该初始化信息作为参数从数据提供程序传递到数据源。

You need a new connection string each time you are connecting to something different (You can have two different DBContexts using the same ConnectionString), in this tutorial although the Data Source of both connection strings is the same, the AttachDbFileName is different. 每次连接到不同的东西时都需要一个新的连接字符串(可以使用相同的ConnectionString拥有两个不同的DBContext),尽管在本教程中,两个连接字符串的数据源都相同,但是AttachDbFileName却不同。

When each DbContext is initialized, it will use one of those connection strings. 初始化每个DbContext时,它将使用这些连接字符串之一。 In this tutorial, the first connection string (Default Connection) is used for membership (user accounts and such) and the other connection string is used for your MovieDBContext, and will contain Movies and other things as you progress in the tutorial. 在本教程中,第一个连接字符串(默认连接)用于成员身份(用户帐户等),而另一个连接字符串用于MovieDBContext,并且在您进行教程时将包含电影和其他内容。

It's also possible to have them both in the same database. 也可以将它们都放在同一个数据库中。

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

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