[英]Using EntityFramework, code-first (CTP5), how do you create the database?
Background: I have LINQ-to-SQL code and all my objects are POCO (plain old CLR objects) that use attributes/data annotations. 背景:我有LINQ-to-SQL代码,我所有的对象都是使用属性/数据注释的POCO(普通的旧CLR对象)。 I have no mapping files. 我没有映射文件。 I am migrating to Entity Framework and would like to use "Code-First" to also generate my database from my domain model (in code). 我正在迁移到Entity Framework,并且想使用“代码优先”来从我的域模型(以代码形式)生成数据库。 I have no .edmx, no .ssdl, etc. type meta files. 我没有.edmx,.ssdl等类型的元文件。
I'm looking for the LINQ-to-SQL equivalent of something like this: 我正在寻找类似LINQ-to-SQL的东西:
DataContext context = new DataContext(connString)
context.GetTable<MyEntity>();
context.CreateDatabase();
Someone asked a similar question here Does the Entity Framework have an equivalent of DataContext.GetTable<TEntity> from Linq2Sql (ObjectContext.CreateQuery<T>?) 有人在这里问了类似的问题实体框架是否具有等效于Linq2Sql的DataContext.GetTable <TEntity>(ObjectContext.CreateQuery <T>?)?
so my code now looks like this: 所以我的代码现在看起来像这样:
ObjectContext oContext = new ObjectContext(connectionString);
if (oContext.DatabaseExists()) oContext.DeleteDatabase();
oContext.CreateQuery<StockOrder>(typeof(StockOrder).Name);
oContext.CreateDatabase();
The problem I am facing now is -- my connection string has to be an "EntityConnectionString", which is different(!) than my current LINQ-2-SQL connection string I am using. 我现在面临的问题是-我的连接字符串必须是“ EntityConnectionString”,它与我正在使用的当前LINQ-2-SQL连接字符串不同(!)。 I feel like I'm going down the wrong path. 我觉得我走错了路。
Is this even the way to do it? 这甚至是做到这一点的方法吗? Or maybe EF4 just doesn't support this yet? 也许EF4尚不支持此功能? I hope I'm wrong, otherwise, I will be (like others) severely disappointed with EF4. 我希望我错了,否则,我(和其他人一样)会对EF4感到非常失望。
Thanks Ray 谢谢雷
Use the EntityConnectionStringBuilder class to create a correct connection. 使用EntityConnectionStringBuilder类创建正确的连接。
The code will look like the following: 该代码将如下所示:
EntityConnectionStringBuilder escb = new EntityConnectionStringBuilder();
escb.Provider = "System.Data.SqlClient";
escb.ProviderConnectionString = connString;
escb.Metadata = "Model1.csdl|Model1.ssdl|Model1.msl"; //set the correct metadata path here
connString = escb.ToString(); //Here is the correct connection string
UPD: Here is a Code First sample: UPD:这是“代码优先”示例:
class Program {
static void Main(string[] args) {
DbDatabase.DefaultConnectionFactory = new System.Data.Entity.Database.SqlConnectionFactory("Data Source=.;Integrated Security=SSPI");
DbDatabase.SetInitializer<StockContext>(new DropCreateDatabaseIfModelChanges<StockContext>());
StockContext oContext = new StockContext();
oContext.StockOrders.Add(new StockOrder{ StockOrderID = 2, Name = "test"});
oContext.SaveChanges();
}
}
public class StockOrder {
public int StockOrderID { get; set; }
public string Name { get; set; }
}
public class StockContext : DbContext {
public DbSet<StockOrder> StockOrders { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.