实体框架 - CTP4 - 代码优先 - 如何关闭自动多元化? [英] Entity Framework - CTP4 - Code First - How to turn off the automatic pluralization?

查看:121
本文介绍了实体框架 - CTP4 - 代码优先 - 如何关闭自动多元化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实体名称是联系人,我的表名称是联系人。然而,默认的多元化支持正在使EF4查找名为Contacts的表。任何人有什么想法如何关闭多元化支持?



这个的帖子已经有了一些关于复合化支持的细节。但是仍然没有给我答案。



我在这个的帖子。首先,我不知道哪个物理的.tt文件需要做这个改变。另外,我希望这个功能仅针对一个应用程序而不是所有的。


T4工具箱中的代码生成器有
在Visual Studio 2010中默认启用
的复数。如果需要
生成没有
复数的DAL,也许是
兼容性原因,您可以将
这个选项通过在
generator.Run()方法调用之前添加
以下行添加到.tt文件中。



C# >
generator.Pluralize = false;



VB

generator.Pluralize = False


***** UPDATE *****



以下是我使用的代码,我收到以下错误: -



联系人

  public class Contact 
{
public int ContactID {get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string标题{get;组; }
public DateTime AddDate {get;组; }
public DateTime ModifiedDate {get;组; }
}

上下文: -



public class AddressBook:DbContext
{
public DbSet< Contact>联系{get;组;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity< Contact>()。MapSingleType()。ToTable(dbo.Contact);
}

}



主要程序: -

  using(var context = new AddressBook())
{
var contact = new Contact
{
ContactID = 10000,
FirstName =Brian,
LastName =Lara,
ModifiedDate = DateTime.Now,
AddDate = DateTime.Now,
Title =Mr.

};
context.Contact.Add(contact);
int result = context.SaveChanges();
Console.WriteLine(Result: - + result.ToString());

}

我在context.Contact上收到以下错误。添加(联系人);: -


System.InvalidOperationException:
模型支持'AddressBook'
上下文自数据库
创建以来,已更改。或者手动
删除/更新数据库,或者使用
IDatabaseInitializer实例调用
Database.SetInitializer。对于
示例,
RecreateDatabaseIfModelChanges
策略将自动删除,
重新创建数据库,并且可选地
将其与新数据进行种子关联。在
System.Data.Entity.Infrastructure.CreateDatabaseOnlyIfNotExists 1.InitializeDatabase(TContext
上下文)在
System.Data.Entity.Infrastructure.Database.Initialize()
at
System.Data.Entity.Internal.InternalContext.Initialize()
at
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type
entityType)at
System.Data.Entity.Internal.Linq.EfInternalQuery
1.Initialize()

System.Data.Entity.DbSet 1 .ActOnSet(Action
action,EntityState newState,TEntity
entity)at
System.Data.Entity.DbSet
1.Add(TEntity
entity)在
CodeFirst.Client.Program.Main(String []
args)在E:\Ashish\Research\VS
Solutions\EntityFramework\CodeFirstApproach_EF_CTP4\CodeFirst.Client \Program.cs:第35行


我相信我在某个地方犯了一个愚蠢的错误,只是无法弄清楚。有人可以提供一些方向?



ANSWER
使用Pault的帮助我描述了这个问题,解决方案这里

解决方案

我正在添加第三个答案,因为我对问题的理解改变了...对我来说是不好的? ;)



像我在评论中说的,我还在学习,我还没有尝试使用现有的数据库。也就是说,希望其中一个有助于:



Scott Guthrie提到的帖子( http://weblogs.asp.net/scottgu/archive/2010/08 /03/using-ef-code-first-with-an-existing-database.aspx )有一个Jeff的评论说,这可以帮助(我建议阅读完整的评论,他更详细地解释):

  Database.SetInitializer< AddressBook>(null); // AddressBook是上下文

我也发生在Rowan Miller在这篇文章下的评论 http://blogs.msdn。 com / b / adonet / archive / 2010/07/14 / ctp4announcement.aspx?PageIndex = 2 )。他建议这可以是一个选项:

  public class ProductContext:DbContext 
{
protected override void OnModelCreating (ModelBuilder modelBuilder)
{
modelBuilder.IncludeMetadataInDatabase = false;
}
...
}

希望其中之一让你在正确的轨道上。


My entity name is "Contact" and my table name is "Contact". However, the default pluralization support is making EF4 to look for a table named "Contacts". Anybody has any idea on how to turn off the pluralization support?

This post has got some details on pluralization support. But still does not give me an answer.

I see the following text in this post. First of all, I dont know which physical .tt file I need to make this change. Also, I want to have this feature turned off only for one app and not for all.

The code generator in T4 Toolbox has the pluralization turned on by default in Visual Studio 2010. If you need to generate the DAL without pluralization, perhaps for compatibility reasons, you can turn this option off by adding the following line to the .tt file before generator.Run() method is called.

C#
generator.Pluralize = false;

VB
generator.Pluralize = False

*****UPDATE*****

Following is the code I use and I get an error given below:-

Contact

 public class Contact
 {
 public int ContactID { get; set; }
 public string FirstName { get; set; }
 public string LastName { get; set; }
 public string Title { get; set; }
 public DateTime AddDate { get; set; }
 public DateTime ModifiedDate { get; set; }
 }

Context:-

 public class AddressBook : DbContext
 {
 public DbSet<Contact> Contact { get; set; }

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
  modelBuilder.Entity<Contact>().MapSingleType().ToTable("dbo.Contact");
 }

}

The main program:-

using (var context = new AddressBook())
  {
   var contact = new Contact
   {
   ContactID = 10000,
   FirstName = "Brian",
   LastName = "Lara",
   ModifiedDate = DateTime.Now,
   AddDate = DateTime.Now,
   Title = "Mr."

   };
   context.Contact.Add(contact);
   int result = context.SaveChanges();
   Console.WriteLine("Result :- " + result.ToString());

  }

And I get the following error on "context.Contact.Add(contact);":-

System.InvalidOperationException: The model backing the 'AddressBook' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the RecreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data. at System.Data.Entity.Infrastructure.CreateDatabaseOnlyIfNotExists1.InitializeDatabase(TContext context) at System.Data.Entity.Infrastructure.Database.Initialize() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.EfInternalQuery1.Initialize() at System.Data.Entity.DbSet1.ActOnSet(Action action,EntityState newState, TEntity entity) at System.Data.Entity.DbSet1.Add(TEntity entity) at CodeFirst.Client.Program.Main(String[] args) in E:\Ashish\Research\VS Solutions\EntityFramework\CodeFirstApproach_EF_CTP4\CodeFirst.Client\Program.cs:line 35

I am sure I am making a stupid mistake somewhere, just unable to figure out. Could somebody please provide some directions?

ANSWER With Pault's help I described this problem and the solution here.

解决方案

I'm adding yet a third answer as my understanding of the question changes... is that bad of me? ;)

Like I said in my comment, I'm still learning and I haven't attempted this with an existing database yet. That said, hopefully one of these will help:

The post I mentioned by Scott Guthrie (http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx) has a comment by a certain Jeff that says this can help (I recommend reading the full comment as he explains in more detail):

Database.SetInitializer<AddressBook>(null); //AddressBook being the context

I've also happened across a comment by Rowan Miller underneath this post (http://blogs.msdn.com/b/adonet/archive/2010/07/14/ctp4announcement.aspx?PageIndex=2) in the recent past. He suggests this may be an option:

public class ProductContext : DbContext
{
   protected override void OnModelCreating(ModelBuilder modelBuilder)
   {
       modelBuilder.IncludeMetadataInDatabase = false;
   }
   ...
}

Hopefully one of these gets you on the right track.

这篇关于实体框架 - CTP4 - 代码优先 - 如何关闭自动多元化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆