代码之家  ›  专栏  ›  技术社区  ›  Jeroen

如何指定实体配置的名称

  •  0
  • Jeroen  · 技术社区  · 14 年前

    是否可以指定实体配置的名称?实际上,将表名从“xxxSet”更改为我选择的名称?

    2 回复  |  直到 14 年前
        1
  •  1
  •   Craig Stuntz    14 年前

    找到 the documentation . 你想做一些类似的事情:

    public class BloggingModel : ObjectContext
    {
        public BloggingModel(EntityConnection connection)
            : base(connection)
        {
            DefaultContainerName = "BloggingModel";
        }
    
        public IObjectSet<User> Users   // ObjectSet name -- you can call it whatever you want
        {
            get { return base.CreateObjectSet<User>(); }
        }
    }
    
    class UserConfiguration : EntityConfiguration<User>
    {
        public UserConfiguration() 
        {
            Property(u => u.Password).HasMaxLength(15).IsRequired();
    
            Relationship(u => u.AuthoredPosts).FromProperty(p => p.Author);
            Relationship(u => u.PostedPosts).FromProperty(p => p.Poster);
    
            MapHierarchy( 
                u => EntityMap.Row( 
                    EntityMap.Column(u.ID, "uid"),
                    EntityMap.Column(u.Password)
                )
            ).ToTable("Users");  // DB table name -- again, anything you like
        }
    }
    

    同样,请参阅链接的文章以获取完整信息。

        2
  •  0
  •   Padel    14 年前

    据我所知,您可以从designer is Visual Studio(最简单的方法)或从.edmx文件(如果您知道您在那里做什么)更改它。另外,请尝试更新到大约2个月前发布的.NET4.0和EF4,这样就不用考虑CTP了(有Visual C#2010或WebDeveloper2010的快速版本,您可以免费使用)。这个版本中的设计者比CTP更“完整”,并且更知道如何自动命名实体(复数)。

    这与EF有关,而不是Linq2Sql(它在某种程度上类似于EF),但我不知道如何确切地更改名称(尽管肯定有一种方法,而且必须与EF的方法非常相同)。