با گزینه Entity Framework\Reverse Enginer Code First از Visual Studio .net می خواهیم کدهای مربوط به جداول پایگاه داده را بسازد. بعد از ساخت وقتی برنامه را اجرا می کنیم و اطلاعات جدولی را درخواست خواندن می کنیم خطای زیر رخ می دهد.
علت چیست و چگونه حل می شود؟
کد مدل ها را قرار دهید تا بتونیم کمک کنیم
سلام
public partial class Order { public Order() { this.OrderBankTransactionNumbers = new List<OrderBankTransactionNumber>(); this.OrderDetails = new List<OrderDetail>(); } public int OrderID { get; set; } public int UserID { get; set; } public bool IsFinaly { get; set; } public System.DateTime OrderDate { get; set; } public Nullable<int> WarehouseID { get; set; } public Nullable<int> WarehouseTransactionID { get; set; } public string ShipName { get; set; } public string ShipCity { get; set; } public string ShipAddress { get; set; } public virtual ICollection<OrderBank> OrderBanks { get; set; } public virtual ICollection<OrderDetail> OrderDetails { get; set; } public virtual User User { get; set; } public virtual Warehouse Warehouse { get; set; } public virtual WarehouseTransaction WarehouseTransaction { get; set; } }
public partial class OrderDetail { public int DetailID { get; set; } public int OrderID { get; set; } public int ProductModelID { get; set; } public int Count { get; set; } public int UnitPrice { get; set; } public double DiscountPercent { get; set; } public int Price { get; set; } public virtual Order Order { get; set; } public virtual ProductModel ProductModel { get; set; } }
و
using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.ModelConfiguration; namespace DiGiShop.Models.Mapping { public class OrderMap : EntityTypeConfiguration<Order> { public OrderMap() { // Primary Key this.HasKey(t => t.OrderID); // Properties this.Property(t => t.ShipName) .HasMaxLength(40); this.Property(t => t.ShipCity) .HasMaxLength(30); this.Property(t => t.ShipAddress) .HasMaxLength(50); // Table & Column Mappings this.ToTable("Orders"); this.Property(t => t.OrderID).HasColumnName("OrderID"); this.Property(t => t.UserID).HasColumnName("UserID"); this.Property(t => t.IsFinaly).HasColumnName("IsFinaly"); this.Property(t => t.OrderDate).HasColumnName("OrderDate"); this.Property(t => t.WarehouseID).HasColumnName("WarehouseID"); this.Property(t => t.WarehouseTransactionID).HasColumnName("WarehouseTransactionID"); this.Property(t => t.ShipName).HasColumnName("ShipName"); this.Property(t => t.ShipCity).HasColumnName("ShipCity"); this.Property(t => t.ShipAddress).HasColumnName("ShipAddress"); // Relationships this.HasRequired(t => t.User) .WithMany(t => t.Orders) .HasForeignKey(d => d.UserID); this.HasOptional(t => t.Warehouse) .WithMany(t => t.Orders) .HasForeignKey(d => d.WarehouseID); this.HasOptional(t => t.WarehouseTransaction) .WithMany(t => t.Orders) .HasForeignKey(d => d.WarehouseTransactionID); } } }
using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity.ModelConfiguration; namespace DiGiShop.Models.Mapping { public class OrderDetailMap : EntityTypeConfiguration<OrderDetail> { public OrderDetailMap() { // Primary Key this.HasKey(t => t.DetailID); // Properties // Table & Column Mappings this.ToTable("OrderDetails"); this.Property(t => t.DetailID).HasColumnName("DetailID"); this.Property(t => t.OrderID).HasColumnName("OrderID"); this.Property(t => t.ProductModelID).HasColumnName("ProductModelID"); this.Property(t => t.Count).HasColumnName("Count"); this.Property(t => t.UnitPrice).HasColumnName("UnitPrice"); this.Property(t => t.DiscountPercent).HasColumnName("DiscountPercent"); this.Property(t => t.Price).HasColumnName("Price"); // Relationships this.HasRequired(t => t.Order) .WithMany(t => t.OrderDetails) .HasForeignKey(d => d.OrderID); this.HasRequired(t => t.ProductModel) .WithMany(t => t.OrderDetails) .HasForeignKey(d => d.ProductModelID); } } }
از orderid خطا میگیره
کدتون رو خوندم ولی مشکلی یه نظرم نیومد
خودتون رابطه های این فیلد رو بررسی کنید
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)