通过UserContext

时间:2015-11-17 16:00:31

标签: .net entity-framework

我有一个从典型模板创建的应用程序,其中包含用于登录等的常规用户标识。它带有预先构建的UserContext,我对其进行了一些修改。我想在数据库中添加另一个表(我使用的是代码优先迁移)。

以下是新表的模型:

[Table("Disclosures")]
public class Disclosure
{
    [Key]
    public int DisclosureId { get; set; }
    public string ActivityTitle { get; set; }
    public string ActivityDate { get; set; }
    public bool FinancialRelationship { get; set; }
    public string GrantsResearchSupport { get; set; }
    public string Consultant { get; set; }
    public string StockShareholder { get; set; }
    public string Honoraria { get; set; }
    public string Other { get; set; }
    public string DateSigned { get; set; }
}

这就是我将它添加到UserContext的方式(这是我不确定我是否正确执行此操作的方式)

public class UserContext : IdentityDbContext<User>
{
    public UserContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<User>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles").Property(p=>p.Id).HasColumnName("RoleId");
    }

    public DbSet<Disclosure> Disclosures { get; set; }

    public static UserContext Create()
    {
        return new UserContext();
    }
}

这是创建后的表结构

CREATE TABLE [dbo].[Disclosures] (
    [DisclosureId]          INT            IDENTITY (1, 1) NOT NULL,
    [ActivityTitle]         NVARCHAR (MAX) NULL,
    [ActivityDate]          NVARCHAR (MAX) NULL,
    [FinancialRelationship] BIT            NOT NULL,
    [GrantsResearchSupport] NVARCHAR (MAX) NULL,
    [Consultant]            NVARCHAR (MAX) NULL,
    [StockShareholder]      NVARCHAR (MAX) NULL,
    [Honoraria]             NVARCHAR (MAX) NULL,
    [Other]                 NVARCHAR (MAX) NULL,
    [DateSigned]            NVARCHAR (MAX) NULL,
    [User_Id]               NVARCHAR (128) NULL
);

注意&#34; User_Id&#34;已添加的字段。该属性不会添加到Disclosure模型中。如果我添加新的Disclosure,我该如何设置User_Id值?而且,我是否首先正确地做到了这一点?我基本上只想在这个应用程序中添加更多表,而不会破坏任何已经自动创建的标识内容。

0 个答案:

没有答案
相关问题