简体   繁体   中英

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.

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.

I updated my Log.cs model and then run a Update-Database -Verbose comand in PMC. 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.

So far so good, until I tried to add a new Log in the LogsController and do a SaveChanges() call. Now it gives me an "Invalid column name 'exampleName'" error for the properties I removed earlier.

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.

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. 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. Tried the solution presented in this other question Entity Framework 5 Code First - How to “start over”? 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:

here's the code for the : DbContext class:

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:

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.

My solution were to add [NotMapped] before the Logs collection and that did the trick.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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