简体   繁体   中英

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.

At minimum you'll need your objects and a DbContext.

a typical set up maybe as follows:

Entities

public class Person {

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

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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