50٪ تخفیف روی تمام دوره‌ها!
پایان تخفیف تا:
مشاهده دوره‌ها
0

Fluent API در core2

سلام

من کد زیر را در mvc.net نوشتهام و بدرستی کارمیکند

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;

namespace Models.EntityModels
{
    /*
     * جدول اطلاعات پایه شهرها 
     * بصورت اتوماتیک ثبت میشود 
     * توسط سیستم برای لود شدن در بار اول
      */
    public class CityType
    {
        public CityType()
        {

        }
        public CityType(string name)
        {
            Name = name;
        }
        internal class Cofiguration : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<CityType>
        {
            public Cofiguration()
            {
                ToTable("CityType", "Base")
                    .HasKey(c => c.CityTypeId)
                    ;
                Property(c => c.CityTypeId)
                    .HasColumnOrder(0)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                    ;
                Property(c => c.Name)
                    .HasColumnOrder(1)
                    .HasColumnName("Name")
                    .HasColumnAnnotation(
                        IndexAnnotation.AnnotationName,
                        new IndexAnnotation(
                            new IndexAttribute("IX_Name", 1) { IsUnique = true }))
                    .IsUnicode(true)
                    .HasMaxLength(15)
                    ;
            }
        }

        public int CityTypeId { get; set; }
        public string Name { get; set; }
        public virtual IList<City> Cities { get; set; }

    }
}

حال می خواهم این کد را در mvc.core2 بنویسم ارور می دهد

The type or namespace name 'Entity' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)

برای رفع خطا باید چکارکنم

باتشکر

پرسیده شده در 1396/08/15 توسط

1 پاسخ

1

سلام، داخل Entity Framework Core کلاً فضای نامی به نام System.Data.Entity نداریم و همه چی به Microsoft.EntityFramework منتقل شده. در ضمن بحث Fluent API هم تغییراتی داشته. شما به جای EntityTypeConfiguration باید کلاسی بسازید که اینترفیس IEntityTypeConfiguration رو پیاده سازی کنه:

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ApplyConfiguration(new CityTypeMap());
        }
    }

    public class CityTypeMap : IEntityTypeConfiguration<CityType>
    {
        public void Configure(EntityTypeBuilder<CityType> builder)
        {
            builder.ToTable("CityType", "Base")
                .HasKey(c => c.CityTypeId)
                ;
            builder.Property(c => c.CityTypeId)
                .ValueGeneratedOnAdd();
            builder.HasIndex(c => c.Name).IsUnique();
            builder.Property(c => c.Name)
                .HasColumnName("Name")
                .IsUnicode()
                .HasMaxLength(15);
        }
    }
پاسخ در 1396/08/15 توسط

پاسخ شما