c# – Entity Framework Core:将上传的文件保存到连接表中

c# – Entity Framework Core:将上传的文件保存到连接表中,第1张

概述我有一个新闻表与一个文件表连接,我想要保存与新闻相关的图像或PDF.下面是我的模型和Create方法,我将发布的文件保存到连接表中. 但是我收到以下错误.这对我没有意义,因为NewsFiles不应该是我数据库中的对象. NewsFile是一个表,NewsFiles是虚拟集合. A database operation failed while processing the request. DbU 我有一个新闻表与一个文件表连接,我想要保存与新闻相关的图像或pdf.下面是我的模型和Create方法,我将发布的文件保存到连接表中.

但是我收到以下错误.这对我没有意义,因为Newsfiles不应该是我数据库中的对象. Newsfile是一个表,Newsfiles是虚拟集合.

A database operation Failed while processing the request.
dbupdateException: An error occurred while updating the entrIEs. See
the inner exception for details. sqlException: InvalID object name
‘Newsfiles’. There are pending model changes for ApplicationDbContext
In Visual Studio,use the Package Manager Console to scaffold a new
migration for these changes and apply them to the database:

PM> Add-Migration [migration name] PM> Update-Database Alternatively,
you can scaffold a new migration and apply it from a command prompt at
your project directory:

dotnet ef migrations add [migration name]
dotnet ef database update

public class News{    [Key] public int ID { get; set; }    public string Title { get; set; }    public virtual ICollection<Newsfile> Newsfiles { get; set; }}public class file{    [Key] public int ID { get; set; }    public string filename { get; set; }    public Byte[] Content { get; set; }    public virtual ICollection<Newsfile> Newsfiles { get; set; }}public class Newsfile{    [Key] public int ID { get; set; }    public int NewsID { get; set; }    public News News { get; set; }    public int fileID { get; set; }    public file file { get; set; }}protected overrIDe voID OnModelCreating(ModelBuilder builder){    base.OnModelCreating(builder);    builder.Entity<Newsfile>().HasIndex(nf => new { nf.NewsID,nf.fileID });    builder.Entity<Newsfile>().HasOne(nf => nf.News).WithMany(n => n.Newsfiles).HasForeignKey(nf => nf.NewsID);    builder.Entity<Newsfile>().HasOne(nf => nf.file).WithMany(c => c.Newsfiles).HasForeignKey(pc => pc.fileID);}[httpPost][ValIDateAntiForgeryToken]public async Task<IActionResult> Create(Newsviewmodel model){    if (ModelState.IsValID)    {        Byte[] file = null;        using (var ms = new MemoryStream())        {            model.file.copyTo(ms);            file = ms.ToArray();        }        model.News.Newsfiles = new List<Newsfile>()        {            new Newsfile()            {                file = new Models.file() { Content = file,filename = model.file.filename }            }        };        _dbContext.News.Add(model.News);        await _dbContext.SaveChangesAsync();        return RedirectToAction("Index");    }    return VIEw(model);}
解决方法 By default:

EF Core will create database tables for all DbSet propertIEs in a context class with the same name as the property

因此,您需要覆盖默认约定.我看到你的评论是builder.Entity< Newsfile>().Totable(“Newsfile”)不起作用.但我只是尝试了它,它解决了这个问题.

当我对表的显式映射进行注释时,我得到了异常sqlException:无效的对象名称’Newsfiles’,但它没有说明挂起的模型更改和迁移.因此,您在某种程度上得到了模型和数据库之间的不一致.正如您在评论中所述,重建数据库可以提供帮助

总结

以上是内存溢出为你收集整理的c# – Entity Framework Core:将上传的文件保存到连接表中全部内容,希望文章能够帮你解决c# – Entity Framework Core:将上传的文件保存到连接表中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1233157.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-06
下一篇2022-06-06

发表评论

登录后才能评论

评论列表(0条)

    保存