[英]How do I map POCO model classes to existing DB tables using Entity Framework
We have existing POCO model classes and an existing DB. 我们有现有的POCO模型类和现有的DB。 The OO model classes and relational DB tables don't map directly to each other.
OO模型类和关系数据库表不直接相互映射。
How do I use Entity Framework to map these? 如何使用Entity Framework映射这些? All of the tutorials I have seen are either Code First (which goes and generates new DB tables), Model first (which generates new DB tables and POCOs) and DB First (which generates new POCOs).
我见过的所有教程都是Code First(生成新的DB表),Model First(生成新的DB表和POCO)和DB First(生成新的POCO)。
I want none of these! 我不想要这些! I want to map between existing POCOs and DB tables.
我想在现有的POCO和数据库表之间进行映射。 What's the best way of doing this?
这样做的最佳方法是什么? Can I use the visual designer, or do I have to do it in code?
我可以使用可视化设计器,还是必须在代码中使用它?
Create mappings which map your entities to database structure (I suggest to use fluent mappings). 创建将实体映射到数据库结构的映射(我建议使用流畅的映射)。 Then just disable database generation:
然后只禁用数据库生成:
Database.SetInitializer<YourContext>(null);
and provide connection string to existing database: 并为现有数据库提供连接字符串:
public class YourContext : DbContext
{
public DatabaseContext(string connectionStringName)
: base(connectionStringName)
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.