Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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# EntityFramework核心可以';t更新数据库“;id';“不是同一类型的”;_C#_Asp.net Core_Entity Framework Core - Fatal编程技术网

C# EntityFramework核心可以';t更新数据库“;id';“不是同一类型的”;

C# EntityFramework核心可以';t更新数据库“;id';“不是同一类型的”;,c#,asp.net-core,entity-framework-core,C#,Asp.net Core,Entity Framework Core,嗨,我正试图用初始迁移更新数据库,但EFCore是这么说的 Column 'Clothes.Id' is not the same data type as referencing column 'Photos.ClothId' in foreign key 'FK_Photos_Clothes_ClothId'. Could not create constraint or index. See previous errors. 这很奇怪,因为即使在创建的迁移中,它也说id是“唯一标识符”。

嗨,我正试图用初始迁移更新数据库,但EFCore是这么说的

Column 'Clothes.Id' is not the same data type as referencing column 'Photos.ClothId' in foreign key 'FK_Photos_Clothes_ClothId'.
Could not create constraint or index. See previous errors.
这很奇怪,因为即使在创建的迁移中,它也说id是“唯一标识符”。 该项目最初是用Asp.Net.Core2.2启动的,但我最近尝试将其更新为3.0。EntityFramework软件包也升级到3.0。也许是某种虫子?谢谢你的帮助:)

UpMethod=>

protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.CreateTable(
名称:“衣服”,
列:表=>new
{
Id=table.Column(类型:“uniqueidentifier”,可为空:false,defaultValueSql:“NEWID()”,
CreatedAt=table.Column(类型:“datetime2”,可为空:false,defaultValueSql:“GETDATE()”,
LastTimeModified=table.Column(类型:“datetime2”,可为空:false,defaultValueSql:“GETDATE()”,
Name=table.Column(可空:false),
Price=table.Column(类型:“decimal(6,2)”,可为空:false),
BoughtOn=table.Column(类型:“datetime2”,可为空:false,defaultValueSql:“DATEADD(day,-1,GETDATE())”,
ClothType=table.Column(可空:false),
大小=表.列(可空:false),
颜色=table.Column(可空:false),
制造商=表.列(可空:false),
ClothUrl=table.Column(可空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_衣服”,x=>x.Id);
});
migrationBuilder.CreateTable(
名称:“照片”,
列:表=>new
{
Id=table.Column(类型:“uniqueidentifier”,可为空:false,defaultValueSql:“NEWID()”,
CreatedAt=table.Column(类型:“datetime2”,可为空:false,defaultValueSql:“GETDATE()”,
LastTimeModified=table.Column(类型:“datetime2”,可为空:false,defaultValueSql:“GETDATE()”,
PhotoUrl=table.Column(类型:“varchar(max)”,可为空:false),
ClothId=table.Column(类型:“varchar(max)”,可为空:false)
},
约束:表=>
{
表.PrimaryKey(“PK_照片”,x=>x.Id);
表1.外键(
名称:“FK\U照片\U衣服\U衣服ID”,
列:x=>x.ClothId,
原则:“衣服”,
主栏:“Id”,
onDelete:引用。级联);
});
migrationBuilder.CreateIndex(
名称:“IX_照片_ClothId”,
表:“照片”,
专栏:“ClothId”,
独特:正确);
}
我正在使用fluentApi提供设置等

公共类WardrobeContext:DbContext
{
公共衣柜上下文(DbContextOptions选项):基本(选项)
{
}
公共数据库设置衣服{get;set;}
公共数据库集照片{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder)
{
builder.ApplyConfiguration(新的BaseEntityConfiguration());
builder.Entity(ConfigureCloth);
builder.ApplyConfiguration(新的BaseEntityConfiguration());
建筑商实体(配置照片);
}
私有void配置布料(EntityTypeBuilder)
{
属性(cloth=>cloth.Name)
.IsRequired(正确);
builder.Property(cloth=>cloth.BoughtOn)
.HasDefaultValueSql(“DATEADD(day,-1,GETDATE())”)
.HasColumnType(“日期时间2”);
builder.Property(cloth=>cloth.ClothType)
.IsRequired(正确);
属性(cloth=>cloth.ClothUrl)
.IsRequired(正确);
builder.Property(cloth=>cloth.Color)
.IsRequired(正确);
builder.Property(cloth=>cloth.Manufacturer)
.IsRequired(正确);
builder.Property(cloth=>cloth.Price)
.IsRequired(正确)
.HasColumnType(“十进制(6,2)”);
builder.Property(cloth=>cloth.Size)
.IsRequired(正确);
builder.HasOne(cloth=>cloth.Photo)
.带一个(x=>x.Cloth)
.HasForeignKey(photo=>photo.ClothId);
}
私有void配置照片(EntityTypeBuilder)
{
builder.Property(photo=>photo.PhotoUrl)
.IsRequired(正确)
.HasColumnType(“varchar(max)”);
builder.Property(photo=>photo.ClothId)
.IsRequired(正确)
.HasColumnType(“varchar(max)”);
builder.HasIndex(ix=>ix.ClothId)
.IsUnique();
}
}
内部类BaseEntityConfiguration:EntityTypeConfiguration其中tenty:BaseEntity
{
公共void配置(EntityTypeBuilder)
{
属性(baseEntity=>baseEntity.Id)
.HasDefaultValueSql(“NEWID()”)
.HasColumnType(“唯一标识符”);
属性(baseEntity=>baseEntity.CreatedAt)
.HasDefaultValueSql(“GETDATE()”)
.HasColumnType(“日期时间2”)
.ValueGeneratedOnAdd();
属性(baseEntity=>baseEntity.LastTimeModified)
.HasDefaultValueSql(“GETDATE()”)
.ValueGeneratedOnAdd()
.HasColumnType(“日期时间2”);
}
}

外键列类型必须与主键相同 键入。更改
internal class BaseEntityConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : BaseEntity
    {
        public void Configure(EntityTypeBuilder<TEntity> builder)
        {
            builder.Property(baseEntity => baseEntity.Id)
                .HasDefaultValueSql("NEWID()")
                .HasColumnType("uniqueidentifier");
            builder.Property(baseEntity => baseEntity.CreatedAt)
                .HasDefaultValueSql("GETDATE()")
                .HasColumnType("datetime2")
                .ValueGeneratedOnAdd();
            builder.Property(baseEntity => baseEntity.LastTimeModified)
                .HasDefaultValueSql("GETDATE()")
                .ValueGeneratedOnAdd()
                .HasColumnType("datetime2");
        }
    }