我首先使用数据库,在我阅读了Entity Framework之后,我决定首先使用VS2015向导(数据库代码优先)转换为代码。
这是我的数据库图表:
生成的代码没有像FinancialSell
中那样映射表.edmx
。但它没有任何配置。这是生成的代码:
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'。
答案 0 :(得分:0)
我很抱歉,但是我无法用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