无效的列名重命名实体框架列

时间:2018-08-11 10:56:35

标签: c# asp.net entity-framework

我正在使用一个asp.net api作为数据库的android应用。我在使用Web服务时出错。

  

发生错误
  无效的列名“ Location_Id”
  无效的列名'ApplicationUser_Id'

我检查了迁移情况,为什么我有Location_Id而不是LocationId?还有User_Id

 AddColumn("dbo.Locations", "Location_Id", c => c.Guid());
 DropColumn("dbo.Locations", "UserId");
 RenameIndex(table: "dbo.Locations", name: "IX_User_Id", newName: "IX_ApplicationUser_Id");
 RenameColumn(table: "dbo.Locations", name: "User_Id", newName: "ApplicationUser_Id");
 CreateIndex("dbo.Locations", "Location_Id");
 AddForeignKey("dbo.Locations", "Location_Id", "dbo.Locations", "Id");

在我的域中,我没有输入带有下划线的LocationId 这是我的位置域:

 [MaxLength(50)]
 public string Name { get; set; }
 [MaxLength(150)]
 public string Address { get; set; }
 [MaxLength(250)]
 public string Description { get; set; }

 public Guid UserId { get; set; }
 public ApplicationUser User { get; set; }

 public virtual ICollection<UserLocation> UserLocations { get; set; }
 public virtual ICollection<Storage> Storages { get; set; }

我的位置上没有Location_Id。只有ID。我不知道如何更改迁移或实体框架中的列。有人可以帮忙吗?

谢谢:)

2 个答案:

答案 0 :(得分:3)

可能您错过了通过ForeignKey属性或fluent mapping建立的表关系。您可以查看this这个问题,可能会有所帮助。

答案 1 :(得分:1)

按照惯例是这样,但是实际上可以通过使用表名的[Table("MyTable")]属性和列名的[Column("ColumnName")]属性来控制它。

这里是一个例子:

[Table("Affiliates")]
public class Affiliates
{
    [Column("AffiliateGUID")]
    public Guid AffiliateId { get; set; }
}