如何指定实体配置的名称

时间:2010-06-29 21:42:26

标签: entity-framework-4

我在实体框架4,实体框架功能CTP 3

中使用代码优先

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

2 个答案:

答案 0 :(得分:1)

找到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
    }
}

再次,请查看链接帖子以获取完整信息。

答案 1 :(得分:0)

我可以从设计器中更改它是Visual Studio(简单方法)或.edmx文件(如果你知道你在那里做什么),据我记得。还尝试更新到大约2个月前发布的.NET 4.0和EF4,所以忘记了CTP(可以免费使用Visual C#2010或Web Developer 2010的快速版本)。此版本中的设计人员比CTP更“完整”,并且更自动地为实体命名(复数化)。

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

干杯!

相关问题