簡體   English   中英

代碼優先生成的代碼不起作用

[英]Code-First generated code doesn't work

我先使用數據庫,在了解了有關Entity Framework的未來之后,我決定先使用VS2015向導(從數據庫進行代碼優先)轉換為代碼。

這是我的數據庫圖:

在此處輸入圖片說明

生成的代碼未像.edmx那樣映射表FinancialSell 但這沒有任何配置。 這是生成的代碼:

public partial class XContext : DbContext
{
    public XContext()
        : base("name=XContext")
    {
    }

    public virtual DbSet<Financial> Financial { get; set; }
    public virtual DbSet<Sell> Sell { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    }
}

[Table("Sell")]
public partial class Sell
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Sell()
    {
        Financial = new HashSet<Financial>();
    }

    public int ID { get; set; }

    public decimal Value { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Financial> Financial { get; set; }
}


[Table("Financial")]
public partial class Financial
{
    public int ID { get; set; }

    public DateTime Date { get; set; }

    public virtual Sell Sell { get; set; }
}

當我嘗試用於簡單的交互時:

 var sell = new Sell {Value = 1.00M};
        var financ = new Financial {Date = DateTime.Now, Sell = sell};

        using(var ctx = new XContext())
        {
            ctx.Financial.Add(financ);
            ctx.SaveChanges();

            Console.WriteLine("Ok Success!");
        }

引發異常:

保存不公開外鍵屬性為其關系的實體時發生錯誤。 EntityEntries屬性將返回null,因為無法將單個實體標識為異常的來源。 通過在實體類型中公開外鍵屬性,可以簡化保存時的異常處理。 有關詳細信息,請參見InnerException。

內部異常

無效的列名“ Sell_ID”。

很抱歉,但是我不能為您提供C#的服務:)但是您應該可以在那里使用一個轉換器來解決問題。

Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Imports System.Data.Entity

Module Module1

    Sub Main()

    Dim sell = New Sell() With {
        .Value = 1D
    }
    Dim financ = New Financial() With {
        .[Date] = DateTime.Now,
        .Sell = sell
    }

    Using ctx = New XContext()
        ctx.Financials.Add(financ)
        ctx.FinancialSells.Add(New FinancialSell With {.Financial = financ, .Sell = sell})
        ctx.SaveChanges()

        Console.WriteLine("Ok Success!")
    End Using
End Sub

End Module
Partial Public Class XContext
Inherits DbContext

Public Sub New()
    'Recreates your database when it changes.
    Database.SetInitializer(Of XContext)(New DropCreateDatabaseIfModelChanges(Of XContext)())
    'MyBase.New("name=XContext")
End Sub

    Public Overridable Property Financials As DbSet(Of Financial)
    Public Overridable Property Sells As DbSet(Of Sell)
    Public Overridable Property FinancialSells As DbSet(Of FinancialSell)

    Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    modelBuilder.Entity(Of FinancialSell)().HasRequired(Function(x) x.Sell).WithMany().WillCascadeOnDelete(False)
End Sub
End Class

<Table("Sell")>
Partial Public Class Sell
    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")>
    Public Sub New()
        Financial = New HashSet(Of Financial)()
    End Sub

    <Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), ScaffoldColumn(False)>
    Public Property ID As Integer
    Public Property Value As Decimal

    <System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")>
    Public Overridable Property Financial As ICollection(Of Financial)

End Class


<Table("Financial")>
Partial Public Class Financial
    <Key, Column(Order:=0), DatabaseGenerated(DatabaseGeneratedOption.Identity), ScaffoldColumn(False)>
    Public Property ID As Integer
    Public Property [Date] As DateTime
    <Column(Order:=1), ForeignKey("Sell")>
    Public Property SellID As Integer

    'Navigation Properties
    Public Overridable Property Sell As Sell

End Class

Partial Public Class FinancialSell
    <Key, Column(Order:=0), ForeignKey("Financial")>
    Public Property FinancialID As Integer
    <Key, Column(Order:=1), ForeignKey("Sell")>
    Public Property SellID As Integer

    'Navigation Properties
    Public Overridable Property Sell As Sell
    Public Overridable Property Financial As Financial
End Class

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM