Tuesday, March 31, 2015

Entity Framework - Error 0019: Each type name in a schema must be unique. Type name was already defined!

I recently used Entity Framework in an Azure WebJob and enjoyed the ease of use so much that I've been slowly converting our web services over to use EF instead of the ADO.NET library we were using before. 

Being that i was about to start writing a "load balancer" of sorts for our Merchant Accounts, i felt this would be the perfect opportunity to make use of EF. 

Once testing the webservices locally proved successful, i published them to my test server. Once i accessed them from there, I kept getting the following error:

The Error 
Schema specified is not valid. Errors: \r\nerror 0194: All artifacts loaded into an ItemCollection must have the same version. Multiple versions were encountered.\r\nRenatus.csdl(708,4) :
error 0019: Each type name in a schema must be unique. Type name `RenatusModel.OrderDetail` was already defined.\r\nRenatus.csdl(725,4) :
error 0019: Each type name in a schema must be unique. Type name `RenatusModel.Order` was already defined.\r\nRenatus.csdl(762,4) :
error 0019: Each type name in a schema must be unique."
Not knowing why i i'd get those errors on the test server, but not locally, i decided to write some Unit Tests to call the Data Layer of my services directly.

public void GetMerchant()
 PaymentDAO dal = new paymentDAO();
 var merchant = dal.GetCurrentMerhant();

public string GetCurrentMerchant()
    var merchant = "Merchant1";

    using (Entities context = new Entities())
        var monthbegin = new DateTime(DateTime.UtcNow.Year,DateTime.UtcNow.Month,1);
        var monthend = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month, 1).AddMonths(1);                    
        var merchants = context.PaymentMerchants.Where(x => x.balance_percentage > 0)
            .Select(x => new
                Merchant = x.db_name,
                Percentage = x.balance_percentage,
                Amount = context.Payments.Where(y => y.IsDeclined == 0 && y.IsCaptured == 1 && y.MerchAcct == x.db_name && y.AddDate > monthbegin && y.AddDate < monthend).Sum(y => y.TotalAmount),
            }).Select(x => new
                Merchant = x.Merchant,
                Percentage = x.Percentage,
                Amount = x.Amount.HasValue ? x.Amount.Value : 20000M

        if (merchants.Count() > 0)
            var total = merchants.Sum(y=> y.Amount);
            merchant = merchants.Select(x =>
                Merchant = x.Merchant,
                Percentage = x.Percentage,
                Amount = x.Amount,
                PercentofTotal = total > 0M && x.Percentage > 0 ? ((x.Amount / total) / x.Percentage) * 100M : 0M
            }).MinBy(x=> x.PercentofTotal).Merchant;
    return merchant;

As you can see, it's pretty straight forward. Running from my Unit.Tests always returned the name of a Merchant. Running from my WCF Services always returned the above error. The Model above doesn't even reference the tables that the above error was referring to -.-

Upon searching google and finding the many potential solutions for this problem, i was lead to look for a reference to another project with an EF model that had the same name. I did find one, so i removed the reference and removed the .dll and .pdb as suggested, and even tried removing my bin and obj folders entirely to let them get repopulated. This, however, DID NOT fix the problem! 

I went back to my Unit Tests to see if the other dll had a similar issue at all, and i found that it was throwing a similar error! UGH! I ran the other test again, just for kicks and giggles and it too was now getting that error where it was previously working! Double UGH! Tested my wcf services locally, in debug mode, and they also had stopped working.

The good news is now i could replicate the error locally, and i could see that although the other model had zero changes made to it since it last worked it was not broken. 

I tried reverting my EF to a previous version wondering if  maybe an update broke the functionality, but that didn't work either.

I made sure all my referenced tables had primary keys on them as suggested by some post online i can no longer find.

I tried deleting my models and recreating them as suggested by another post online.

What FINALLY worked? I recreated one of the models making the EF Model name and the EF Entities name unique to the namespace containing it. 

Amusingly, the old model that wasn't working still didn't work. I checked for old dlls and pdbs that may be referencing the library that was causing conflict but i couldn't find anything, so i just recreated that EF Model using a unique name as well! 

Best Practices for me on this moving forward will be to always make the EF Models uniquely named to the namespace they're in!

1 comment :

Travis Smith said...

I was recommended this blog by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You're wonderful! Thanks! ameritrade log in