مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

rezapajhouhesh

عضویت از 1395/03/30

علت خطا در روش Code First

  • جمعه 5 آذر 1395
  • 09:23
تشکر میکنم

با گزینه Entity Framework\Reverse Enginer Code First از Visual Studio .net می خواهیم کدهای مربوط به جداول پایگاه داده را بسازد. بعد از ساخت وقتی برنامه را اجرا می کنیم و اطلاعات جدولی را درخواست خواندن می کنیم خطای زیر رخ می دهد.

The property 'OrderID' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<T> where T is a valid entity type

علت چیست و چگونه حل می شود؟

پاسخ های این پرسش

تعداد پاسخ ها : 4 پاسخ
کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • جمعه 5 آذر 1395
  • 12:35

کد مدل ها را قرار دهید تا بتونیم کمک کنیم

کاربر سایت

rezapajhouhesh

عضویت از 1395/03/30

  • جمعه 5 آذر 1395
  • 22:56

سلام

    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; }
    }

کاربر سایت

rezapajhouhesh

عضویت از 1395/03/30

  • جمعه 5 آذر 1395
  • 23:03

و

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);

        }
    }
}

کاربر سایت

ایمان مدائنی

عضویت از 1392/01/20

  • شنبه 6 آذر 1395
  • 13:59

از orderid خطا میگیره

کدتون رو خوندم ولی مشکلی یه نظرم نیومد

خودتون رابطه های این فیلد رو بررسی کنید

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)