我有一个数据库优先的应用程序,其中的表与我的模型名称不同。我的应用程序使用 fluent API 运行良好,直到我添加了一个与另一个模型具有一对多关系的模型。然后它开始给我错误:
<块引用>SqliteException:SQLite 错误 1:'没有这样的列:a0.AuthorAuthorId
示例类将是:
class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public Author Author {get; set; }
}
class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
public ICollection<Book> Books { get; set; }
}
而我的 OnModelCreating 是 -
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Book>()
.ToTable("tbl_Book")
.HasKey(b => b.BookId);
modelBuilder.Entity<Author>()
.ToTable("tbl_Author")
.HasKey(a => a.AuthorId);
}
我尝试使用 .HasOne()
和 .WithMany()
来指定表之间的关系。那没有用。
答案 0 :(得分:2)
您尚未定义和配置外键属性,因此 EF 正在寻找具有自己命名约定的属性。为避免该问题在 --unhandled-rejections=strict
模型中添加外键属性 -
@Injectable()
export class towelService {
constructor( @Inject('RabbitMQModule') private readonly client: ClientProxy,) {}
//async run(data: any): Promise<any> {
//CHANGED TO Just
run(data: any)
//DELETED tp return new Promise(async //(resolve,reject) => {
const response = true;
let pattern;
this.client.send(pattern, data);
data['users'] = 'new';
// DELETED resolve(data);
};
}
}
并通过将以下代码添加到 Book
方法中手动配置 -
class Book
{
public int BookId { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public Author Author {get; set; }
}