coolblue2000
coolblue2000

Reputation: 4878

Auto Generating partial classes

This is my first time using EF in VS2012 as I have been using 2010 for up until now. I have added the entity framework model and it adds 2 files with the extension .tt which I am sure was not present in VS2010. Under one of these it generates partial classes to match the entities. However I already have these partial classes in another manually created folder called Entites under the root of my app. This causes an issue on build as they conflict...

How do I either either stop them autogenerating or how do I make them play nice with my manually created partial classes? It is incredibly annoying that VS2012 does this without asking as it breaks my code!

Example of Auto Generated class

namespace StatisticsServer
{
    using System;
    using System.Collections.Generic;

    public partial class Statistic
    {
        public int StatID { get; set; }
        public int CategoryID { get; set; }
        public int FranchiseID { get; set; }
        public double StatValue { get; set; }
    }
}

Example of Manually created class

namespace StatisticsServer.Entities
{
    public partial class Statistic
    {
        public static List<Statistic> GetStatisticsSet(int categoryID)
        {
            List<Statistic> statSet = new List<Statistic>();
            using (var context = new StatisticsTestEntities())
            {
                statSet = (from s in context.Statistics where s.CategoryID == categoryID select s).ToList();
            }
            return statSet;
        }
    }
}

Upvotes: 0

Views: 1935

Answers (1)

Tom van Enckevort
Tom van Enckevort

Reputation: 4198

Make sure that your manually created classes are in the same namespace as the auto-generated ones.

Otherwise the two classes will be seen as separate partial classes, and if you use both namespaces in the same calling class it cannot determine which class you mean.

So for example in your case you might have:

using StatisticsServer;
using StatisticsServer.Entities;

When you then declare an object of the type Statistic in that class the build will fail because the Statistic class exists in both namespaces.

Upvotes: 1

Related Questions