Entity framework c#实体框架外键

Entity framework c#实体框架外键,entity-framework,foreign-keys,Entity Framework,Foreign Keys,我是新来的,希望我能得到一个答案-我从两天开始在网上搜索 这是我第一次在Windows10UWP中使用实体framewort。我必须创建数据库的对象类。在一个对象中,我使用另一个对象类型的属性。 如果我尝试添加一条记录,我将得到一个错误 public class budgetcontext : DbContext { private string myDB = "Filename=budget_4.db"; public DbSet<category> Catego

我是新来的,希望我能得到一个答案-我从两天开始在网上搜索

这是我第一次在Windows10UWP中使用实体framewort。我必须创建数据库的对象类。在一个对象中,我使用另一个对象类型的属性。 如果我尝试添加一条记录,我将得到一个错误

public class budgetcontext : DbContext
{
    private string myDB = "Filename=budget_4.db";

    public DbSet<category> Categories { get; set; }
    public DbSet<transaction> Transactions { get; set; }

    public budgetcontext()
    {
        this.Database.Migrate();
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite(myDB);
    }
}

public class category
{
    private string myGuid = Guid.NewGuid().ToString();

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public double BudgetDaily { get; set; }
    public double BudgetWeekly { get; set; }
    public double BudgetMonthly { get; set; }
    public double BudgetYearly { get; set; }

    public string CategoryGuid
    {
        get
        {
            return myGuid;
        }
    }

}

public class transaction
{
    private string myGuid = Guid.NewGuid().ToString();

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int64 TransactionId { get; set; }
    public string Text { get; set; }
    public category Category { get; set; }
    public DateTime TransDateTime { get; set; }
    public double Amount { get; set; }
}
以下是迁移:

[DbContext(typeof(budgetcontext))]
partial class budgetcontextModelSnapshot : ModelSnapshot
{
    protected override void BuildModel(ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasAnnotation("ProductVersion", "1.1.0-rtm-22752");

        modelBuilder.Entity("budget_sqlite.category", b =>
            {
                b.Property<int>("CategoryId")
                    .ValueGeneratedOnAdd();

                b.Property<double>("BudgetDaily");

                b.Property<double>("BudgetMonthly");

                b.Property<double>("BudgetWeekly");

                b.Property<double>("BudgetYearly");

                b.Property<string>("Name");

                b.HasKey("CategoryId");

                b.ToTable("Categories");
            });

        modelBuilder.Entity("budget_sqlite.transaction", b =>
            {
                b.Property<long>("TransactionId")
                    .ValueGeneratedOnAdd();

                b.Property<double>("Amount");

                b.Property<int?>("CategoryId");

                b.Property<string>("Text");

                b.Property<DateTime>("TransDateTime");

                b.HasKey("TransactionId");

                b.HasIndex("CategoryId");

                b.ToTable("Transactions");
            });

        modelBuilder.Entity("budget_sqlite.transaction", b =>
            {
                b.HasOne("budget_sqlite.category", "Category")
                    .WithMany()
                    .HasForeignKey("CategoryId");
            });
    }
}

public partial class V0001 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Categories",
            columns: table => new
            {
                CategoryId = table.Column<int>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                BudgetDaily = table.Column<double>(nullable: false),
                BudgetMonthly = table.Column<double>(nullable: false),
                BudgetWeekly = table.Column<double>(nullable: false),
                BudgetYearly = table.Column<double>(nullable: false),
                Name = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Categories", x => x.CategoryId);
            });

        migrationBuilder.CreateTable(
            name: "Transactions",
            columns: table => new
            {
                TransactionId = table.Column<long>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Amount = table.Column<double>(nullable: false),
                CategoryId = table.Column<int>(nullable: true),
                Text = table.Column<string>(nullable: true),
                TransDateTime = table.Column<DateTime>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Transactions", x => x.TransactionId);
                table.ForeignKey(
                    name: "FK_Transactions_Categories_CategoryId",
                    column: x => x.CategoryId,
                    principalTable: "Categories",
                    principalColumn: "CategoryId",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Transactions_CategoryId",
            table: "Transactions",
            column: "CategoryId");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Transactions");

        migrationBuilder.DropTable(
            name: "Categories");
    }
}
[DbContext(typeof(budgetcontext))]
部分类budgetcontextModelSnapshot:ModelSnapshot
{
受保护的替代无效构建模型(ModelBuilder ModelBuilder)
{
建模者
.HasAnnotation(“产品版本”、“1.1.0-rtm-22752”);
实体(“预算\ sqlite.category”,b=>
{
b、 财产(“类别”)
.ValueGeneratedOnAdd();
b、 财产(“每日预算”);
b、 财产(“预算月”);
b、 不动产(“BudgetWeekly”);
b、 财产(“预算提前”);
b、 财产(“名称”);
b、 HasKey(“分类”);
b、 ToTable(“类别”);
});
实体(“预算\ sqlite.transaction”,b=>
{
b、 属性(“TransactionId”)
.ValueGeneratedOnAdd();
b、 财产(“金额”);
b、 财产(“分类”);
b、 财产(“文本”);
b、 财产(“TransDateTime”);
b、 HasKey(“交易ID”);
b、 HasIndex(“分类”);
b、 可转帐(“交易”);
});
实体(“预算\ sqlite.transaction”,b=>
{
b、 HasOne(“预算”类、“分类”)
.有很多
.HasForeignKey(“分类”);
});
}
}
公共部分类V0001:迁移
{
受保护的覆盖作废(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“类别”,
列:表=>new
{
CategoryId=table.Column(可空:false)
.Annotation(“Sqlite:Autoincrement”,true),
BudgetDaily=table.Column(可空:false),
BudgetMonthly=table.Column(可空:false),
BudgetWeekly=table.Column(可空:false),
BudgetYearly=table.Column(可空:false),
Name=table.Column(可空:true)
},
约束:表=>
{
表.PrimaryKey(“PK_类别”,x=>x.CategoryId);
});
migrationBuilder.CreateTable(
名称:“交易”,
列:表=>new
{
TransactionId=table.Column(可空:false)
.Annotation(“Sqlite:Autoincrement”,true),
金额=表.列(可空:false),
CategoryId=table.Column(可空:true),
Text=table.Column(可空:true),
TransDateTime=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_事务”,x=>x.TransactionId);
表1.外键(
名称:“FK\U交易\类别\类别ID”,
列:x=>x.CategoryId,
原则性:“类别”,
主栏:“类别ID”,
onDelete:referentialiction.Restrict);
});
migrationBuilder.CreateIndex(
名称:“IX_交易分类”,
表:“交易”,
列:“类别ID”);
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropTable(
名称:“交易”);
migrationBuilder.DropTable(
名称:“类别”);
}
}

非常感谢您的帮助。

错误清楚地表明了问题所在。您尝试使用exist CategoryI添加类别我不想添加类别-它应该只是对现有类别的引用。谢谢您的回答。我能解决这个问题。问题是我使用了另一个上下文中“Category”类型的对象。
[DbContext(typeof(budgetcontext))]
partial class budgetcontextModelSnapshot : ModelSnapshot
{
    protected override void BuildModel(ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasAnnotation("ProductVersion", "1.1.0-rtm-22752");

        modelBuilder.Entity("budget_sqlite.category", b =>
            {
                b.Property<int>("CategoryId")
                    .ValueGeneratedOnAdd();

                b.Property<double>("BudgetDaily");

                b.Property<double>("BudgetMonthly");

                b.Property<double>("BudgetWeekly");

                b.Property<double>("BudgetYearly");

                b.Property<string>("Name");

                b.HasKey("CategoryId");

                b.ToTable("Categories");
            });

        modelBuilder.Entity("budget_sqlite.transaction", b =>
            {
                b.Property<long>("TransactionId")
                    .ValueGeneratedOnAdd();

                b.Property<double>("Amount");

                b.Property<int?>("CategoryId");

                b.Property<string>("Text");

                b.Property<DateTime>("TransDateTime");

                b.HasKey("TransactionId");

                b.HasIndex("CategoryId");

                b.ToTable("Transactions");
            });

        modelBuilder.Entity("budget_sqlite.transaction", b =>
            {
                b.HasOne("budget_sqlite.category", "Category")
                    .WithMany()
                    .HasForeignKey("CategoryId");
            });
    }
}

public partial class V0001 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Categories",
            columns: table => new
            {
                CategoryId = table.Column<int>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                BudgetDaily = table.Column<double>(nullable: false),
                BudgetMonthly = table.Column<double>(nullable: false),
                BudgetWeekly = table.Column<double>(nullable: false),
                BudgetYearly = table.Column<double>(nullable: false),
                Name = table.Column<string>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Categories", x => x.CategoryId);
            });

        migrationBuilder.CreateTable(
            name: "Transactions",
            columns: table => new
            {
                TransactionId = table.Column<long>(nullable: false)
                    .Annotation("Sqlite:Autoincrement", true),
                Amount = table.Column<double>(nullable: false),
                CategoryId = table.Column<int>(nullable: true),
                Text = table.Column<string>(nullable: true),
                TransDateTime = table.Column<DateTime>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Transactions", x => x.TransactionId);
                table.ForeignKey(
                    name: "FK_Transactions_Categories_CategoryId",
                    column: x => x.CategoryId,
                    principalTable: "Categories",
                    principalColumn: "CategoryId",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Transactions_CategoryId",
            table: "Transactions",
            column: "CategoryId");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Transactions");

        migrationBuilder.DropTable(
            name: "Categories");
    }
}