Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET核心实体框架Fluent api错误,DateTime属性将文本作为列类型_C#_Entity Framework_.net Core_Entity Framework Core_Entity Framework Migrations - Fatal编程技术网

C# .NET核心实体框架Fluent api错误,DateTime属性将文本作为列类型

C# .NET核心实体框架Fluent api错误,DateTime属性将文本作为列类型,c#,entity-framework,.net-core,entity-framework-core,entity-framework-migrations,C#,Entity Framework,.net Core,Entity Framework Core,Entity Framework Migrations,在我的模型名称空间中,我有不同的模型类。我在不同的类中有DateTime字段。当我跑的时候 dotnet ef migrations add InitialCreate 毫无疑问,它添加了一个迁移,我也能够更新数据库 当我查找我的initialCommit.Designer.cs时,我发现我所有的DateTime属性都有列类型文本 像这样: namespace myapp.API.Models { public class User { public int

在我的模型名称空间中,我有不同的模型类。我在不同的类中有
DateTime
字段。当我跑的时候

dotnet ef migrations add InitialCreate 
毫无疑问,它添加了一个迁移,我也能够更新数据库

当我查找我的
initialCommit.Designer.cs
时,我发现我所有的
DateTime
属性都有列类型文本

像这样:

namespace myapp.API.Models
{
    public class User
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Gender { get; set; }
        public DateTime BirthDate { get; set; }
        public string KnownAs { get; set; }
        public DateTime CreatedOn { get; set; }
        public DateTime LastActive { get; set; }
        ..
    }
}

namespace myapp.API.Models
{
    public class Photo
    {
        public int Id { get; set; }
        public string Url { get; set; }
        public string Description { get; set; }
        public DateTime DateAdded { get; set; }
        public bool IsMain { get; set; }
        public string PublicId  { get; set; }
        public User User { get; set; }
        public int UserId { get; set; }
    }
}

namespace myapp.API.Migrations
{
    [DbContext(typeof(DataContext))]
    [Migration("20200206201625_initialCommit")]
    partial class initialCommit
    {
        protected override void BuildTargetModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "3.1.1");

            modelBuilder.Entity("myapp.API.Models.Photo", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<DateTime>("DateAdded")
                        .HasColumnType("TEXT");

                    b.Property<string>("Description")
                        .HasColumnType("TEXT");

                    b.Property<bool>("IsMain")
                        .HasColumnType("INTEGER");

                    b.Property<string>("PublicId")
                        .HasColumnType("TEXT");

                    b.Property<string>("Url")
                        .HasColumnType("TEXT");

                    b.Property<int>("UserId")
                        .HasColumnType("INTEGER");

                    b.HasKey("Id");

                    b.HasIndex("UserId");

                    b.ToTable("Photos");
                });

            modelBuilder.Entity("myapp.API.Models.User", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<DateTime>("BirthDate")
                        .HasColumnType("TEXT");

                    b.Property<string>("City")
                        .HasColumnType("TEXT");

                    b.Property<string>("Country")
                        .HasColumnType("TEXT");

                    b.Property<DateTime>("CreatedOn")
                        .HasColumnType("TEXT");

                    b.Property<string>("Gender")
                        .HasColumnType("TEXT");

                    b.Property<string>("Interests")
                        .HasColumnType("TEXT");

                    b.Property<string>("Introduction")
                        .HasColumnType("TEXT");

                    b.Property<string>("KnownAs")
                        .HasColumnType("TEXT");

                    b.Property<DateTime>("LastActive")
                        .HasColumnType("TEXT");

                    b.Property<string>("LookingFor")
                        .HasColumnType("TEXT");

                    b.Property<byte[]>("PasswordHash")
                        .HasColumnType("BLOB");

                    b.Property<byte[]>("PasswordSalt")
                        .HasColumnType("BLOB");

                    b.Property<string>("UserName")
                        .HasColumnType("TEXT");

                    b.HasKey("Id");

                    b.ToTable("Users");
                });

            modelBuilder.Entity("myapp.API.Models.Value", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("INTEGER");

                    b.Property<string>("Name")
                        .HasColumnType("TEXT");

                    b.Property<string>("Type")
                        .HasColumnType("TEXT");

                    b.HasKey("Id");

                    b.ToTable("Values");
                });

            modelBuilder.Entity("myapp.API.Models.Photo", b =>
                {
                    b.HasOne("myapp.API.Models.User", "User")
                        .WithMany("Photos")
                        .HasForeignKey("UserId")
                        .OnDelete(DeleteBehavior.Cascade)
                        .IsRequired();
                });
#pragma warning restore 612, 618
        }
    }
}

您可以自己设置columtype

modelBuilder.Entity<User>.Property<b=>BirthDate>().HasColumnType("date");
modelBuilder.Entity.PropertyBirthDate>().HasColumnType(“日期”);

您的目标数据库是什么?SQLite是唯一一个可以这样做的,因为它。日期存储为ISO8601日期文本。如果您使用SQLite,您使用的是哪个提供商?您好,我使用SQLite进行开发,在生产中我已切换到MySql,在发生此错误后,我再次删除了数据库添加迁移,但我没有删除我的迁移文件夹。我是否需要删除迁移文件夹并删除数据库?如果目标是新数据库,则不需要迁移。你从零开始。您也不能对不同的产品使用相同的迁移,当它们指定特定于产品的类型时就不行了。不同的数据库有不同的类型。没有数据库是完全符合ANSI-SQL的-这是不可能做到的。标准是巨大的,由委员会驱动。所有供应商都有额外的类型和功能。您的目的是什么?对于Mysql数据库,我需要删除所有迁移并再次添加新迁移?顺便问一下,您的意思是这是哪个提供程序?services.AddDbContext(prm=>prm.UseSqlite(Configuration.GetConnectionString(“DefaultConnection”));services.AddControllers().AddNewtonsoftJson(opt=>{opt.SerializerSettings.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore;});我不想每次添加迁移时都手动更改迁移。我想找出问题的主要原因。@FurkanKaracan这个问题是错误的类型,试图使用为一个数据库产品创建的迁移来处理一个完全不同的数据库产品。不同的产品需要不同的迁移。没有完全符合ANSI-SQL的数据库-这是不可能的。
modelBuilder.Entity<User>.Property<b=>BirthDate>().HasColumnType("date");