简体   繁体   English

C#.Net类库数据库架构创建

[英]C# .Net class library database schema create

Im creating a class library that will be used in several projects. 我创建了一个将在多个项目中使用的类库。 This library includes a handfull of entities with associated tables in the database. 该库包括数据库中的实体和关联表的实体。 My question is: how do I create these tables when I include this library in a project? 我的问题是:当我将此库包含在项目中时,如何创建这些表?

I suspect you want a library of objects that are used to generate a database? 我怀疑您想要一个用于生成数据库的对象库吗?

If so you can achieve this with EntityFramework CodeFirst. 如果是这样,您可以使用EntityFramework CodeFirst实现。

At minimum you'll need your objects and a DbContext. 至少您将需要对象和DbContext。

a typical set up maybe as follows: 典型的设置可能如下:

Entities 实体

public class Person {

    public string FirstName { get; set; }
    public string LastName { get; set; }
}

DbContext DbContext

public class MyDbContext : System.Data.Entity.DbContext {

     public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)

     public DbSet<Person> People { get; set; }
}

These would live in your project and when you add a reference to it, for example a web project. 这些将存在于您的项目中,并且当您添加对它的引用时(例如,Web项目)。 There are different ways to build the Database, you could call the constructor (MyDbContext) from your web project and pass a connection string to a server. 有多种构建数据库的方法,您可以从Web项目中调用构造函数(MyDbContext)并将连接字符串传递给服务器。

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

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