[英]Determine SQL schema to DbSet object in EF6
I have code 我有代码
const string schamaName = "schema";
public class Data: DbContext
{
public DbSet<Offer> Offers {get;set;}
public DbSet<Place> Places{get;set;}
}
public class Offer
{
public int Id {get;set;}
public string Name {get;set;}
IEnumerable<Place> Places {get;set;}
}
public class Place
{
public int Id {get;set;}
public string Name {get;set;}
}
This code will generate me three tables one for Offer, one for Place and one for mapping between two previous tables. 此代码将为我生成三个表,一个用于Offer,一个用于Place,一个用于在前两个表之间进行映射。
Now I get wanted structure, but in dbo schema. 现在,我得到了想要的结构,但是在dbo模式中。
I want to put table Offer and mapping inside "offer" schema, third table in "dict" schema How can I do this? 我想在“要约”模式中放置“ Offer”和“映射”表,在“ dict”模式中放置第三个表。该如何做?
I use EF6. 我使用EF6。
I prefer to write some code inside OnCreatingModel method than use attibutes 与使用attibutes相比,我更喜欢在OnCreatingModel方法中编写一些代码
I believe you should be including Offers as a virtual Collection<>, not a DbSet<>: 我相信您应该将商品作为虚拟Collection <>而不是DbSet <>包括在内:
public class Data
{
public int Id {get;set;}
public string Name {get;set;}
public virtual Collection<Offer> Offers {get; set;}
}
Also, if your schema is really "schema" instead of "dbo" then you'll need to decorate your class as follows: 另外,如果您的架构实际上是“ schema”而不是“ dbo”,那么您将需要如下装饰类:
[Table("Data", Schema="schema"]
public class Data
{
...
Or if you prefer OnModelCreating(): 或者,如果您更喜欢OnModelCreating():
modelBuilder.Entity<Data>().ToTable("Data", "schema");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.