简体   繁体   English

数据库中的实体框架不一致(代码优先)

[英]entity framework inconsistency in database (code first)

I'm using the code first utility from EF to assist me build a simple ASP.MVC web API where I'll keep some personal logs about another project development. 我使用EF的代码优先实用程序来帮助我构建一个简单的ASP.MVC Web API,在其中我将保存一些有关另一个项目开发的个人日志。

When I first started creating this, I gave the Log.cs model a few properties I thought it would be necessary and later on I removed them, since they were reduntent. 刚开始创建此属性时,我为Log.cs模型提供了一些我认为必要的属性,后来我删除了它们,因为它们是多余的。

I updated my Log.cs model and then run a Update-Database -Verbose comand in PMC. 我更新了Log.cs模型,然后在PMC中运行Update-Database -Verbose comand。 Then when I went to check the Logs table in the Database I noticed that the migration hadn't remove the columns, so I did it manually: Removed the columns, the FK and the Indexes associated with them. 然后,当我去检查数据库中的Logs表时,我注意到迁移并没有删除列,所以我手动进行了:删除了列,FK和与之关联的索引。

So far so good, until I tried to add a new Log in the LogsController and do a SaveChanges() call. 到目前为止一切顺利,直到我尝试在LogsController中添加新的Log并执行SaveChanges()调用为止。 Now it gives me an "Invalid column name 'exampleName'" error for the properties I removed earlier. 现在,我之前删除的属性给我一个“无效的列名'exampleName'”错误。

Doing a solution search for the properties I didn't find them anywhere (so I could delete them from any script looking for them). 在执行解决方案搜索时,我找不到在任何地方都存在的属性(因此我可以从寻找它们的任何脚本中将其删除)。

I have AutomaticMigrationsEnabled and AutomaticMigrationDataLossAllowed set to true during the whole devolement. 在整个开发过程中,我将AutomaticMigrationsEnabled和AutomaticMigrationDataLossAllowed设置为true。

Since my english is very poor, let me put some code to help illustrate the problem. 由于我的英语很差,所以让我放一些代码来帮助说明问题。

The Model: 该模型:

public class Log
{
    public int Id { get; set; }
    public int OwnerTable { get; set; }
    public int OwnerCode { get; set; }
    public int OwnerId { get; set; }
    public int Hidden { get; set; }
    public string ModificationShort { get; set; }
    public string ModificationLong { get; set; }
    public string Version { get; set; }
    public string Stuff { get; set; }
    public DateTime Date { get; set; }
}

The controller where the error is being generated (keep in mind that the Log doesn't have its own controller, since the model is used to keep information about the changed done in the other models): 产生错误的控制器(请注意,日志没有自己的控制器,因为该模型用于保留有关其他模型中所做更改的信息):

    [HttpPost]
    public ActionResult New(Element elem)
    {
        if(ModelState.IsValid)
        {
            elem.Date = DateTime.Now;
            try
            {
                elem.Code = _db.Elements.OrderByDescending(r => r.Code).ToList().Count + 1;
            }
            catch (Exception)
            {
                elem.Code = 1;
            }

            elem.Logs = new List<Log>();
            elem.Logs.Add(new Log
            {
                Date = DateTime.Now,
                Hidden = 0,
                ModificationShort = "Element criation",
                ModificationLong = "Explanation why the element was added to the project",
                Version = "0.1",
                Stuff = "aditional information that might be usefull later on",
                OwnerTable = Resources.Constants.TABBLE_ELEMENTS,
                OwnerCode = elem.Code,
                OwnerId = elem.Id
            });
            _db.Elements.Add(elem);
            _db.SaveChanges();
            return RedirectToAction("Index", new { name = elem.Name });
        }
        return View(elem);
    }

Please ignore all other erros in the code that aren't relevant to the question, since this is an work in progress and I know there are a lot wrong with it. 请忽略代码中与该问题无关的所有其他错误,因为这是一项正在进行中的工作,我知道它存在很多错误。 I just can't until this is fixed. 在解决此问题之前,我无法做到。

Also, let me know if I can provide any more info to assist in a better answer. 另外,请告诉我是否可以提供更多信息,以帮助您获得更好的答案。

Thank you in advance. 先感谢您。

UPDATE: 更新:

Forgot to say what I tried already. 忘记说我已经尝试过了。

Tried to completely delete the database (the .mdf file) and update again. 试图完全删除数据库(.mdf文件)并再次更新。 Tried delete the Migrations folder from the project and enable it again in the PMC so it would create new scripts with the corrent Models. 尝试从项目中删除“迁移”文件夹,然后在PMC中再次启用它,以便它将使用相应的模型创建新脚本。 Tried the solution presented in this other question Entity Framework 5 Code First - How to “start over”? 尝试了另一个问题中提出的解决方案Entity Framework 5代码优先-如何“重新开始”? Tried to delete every occurrence of the removed properties from every file in the solution (used the solution search, don't know if exists still any references to it somewhere the solution search tool can't reach) 尝试从解决方案中的每个文件中删除每次出现的已删除属性(使用解决方案搜索,不知道在解决方案搜索工具无法到达的某个位置是否仍然存在对它的任何引用)

Update2: UPDATE2:

here's the code for the : DbContext class: 这是:DbContext类的代码:

public class GoPDB : DbContext
    {
        public GoPDB() : base("name=DefaultConnection")
        {

        }
        public DbSet<Ability> Abilities { get; set; }
        public DbSet<Element> Elements { get; set; }
        public DbSet<ElementCard> ElementCards { get; set; }
        public DbSet<Log> Logs { get; set; }
        public DbSet<Piece> Pieces { get; set; }
    }

Finally found what I did wrong. 终于发现我做错了。 As I mentioned in the question, the other models in the project need to use the Log model to keep their logs, so the way I designed the project was to keep a ICollection of Log in each of the other models, like so: 正如我在问题中提到的那样,项目中的其他模型需要使用Log模型来保存其日志,因此我设计项目的方式是在其他每个模型中都保留Log的ICollection,如下所示:

public int Id { get; set; }
public int Code { get; set; }
public int NumberOfCards { get; set; }
public string Name { get; set; }
public string Theme { get; set; }
public Combinations Combs { get; set; }
public ICollection<ElementCard> Cards { get; set; }
public ICollection<Log> Logs { get; set; }
public DateTime Date { get; set; }

Now the problem seems to be that whe nyou have a list of something in your model, the EF will reference it somewhere in the database. 现在的问题似乎是您的模型中有一些东西的清单,EF会在数据库中的某个地方引用它。

My solution were to add [NotMapped] before the Logs collection and that did the trick. 我的解决方案是在Logs集合之前添加[NotMapped],就可以了。

Unfortunately I dont understand enough of the framework to explain it better then this. 不幸的是,我对框架的理解不足,无法对此做更好的解释。 If someone can post an answer with a better explaination I would apretiate it. 如果有人可以用更好的解释来发布答案,我将予以证明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM