none
Embedded resource already added error RRS feed

  • Question

  •  

    I have an external mapping file which resides in this.that.Data. I reference this.that.Data project in a website.

    The mapping file is marked as an embedded resource. I created a class that inherits from DataContext and in it's constructor I place a ref to the connectionstring and mapping file I want to use.

     See code:

     

    public class CesDataContext : DataContext

    {

    public CesDataContext() : base(Constants.GetConnectionString(Constants.ConnectionStringType.MainDb),

    XmlMappingSource.FromStream(typeof(CesDataContext).Assembly.GetManifestResourceStream("this.that.data.mapping.xml"))) {}

     

    }

     

    I am trying to use a singleton factory like this

    public class CesDataContextFactory :DataContextFactory

    {

    private static CesDataContextFactory _instance;

    private DataContext _dc;

    protected CesDataContextFactory()

    {

    _dc = CreateDataContext();

    }

    public static CesDataContextFactory GetInstance()

    {

    if (_instance == null)

    {

    _instance = new CesDataContextFactory();

    }

    return _instance;

    }

    public override DataContext CreateDataContext()

    {

    return new CesDataContext();

    }

    public CesDataContext DataContext

    {

    get { return (CesDataContext)_dc; }

    }

    }

     

    So when I want to use the the DataContext I can just access it through the factory like a Facade.

    CesDataContextFactory.GetInstance().DataContext.DoSomething()

    but I am getting an error on the page that uses it. Apparently I am adding the mapping reference over and over and I get An item with the same key has already been added.

     

    Any ideas what I am doing wrong? Is there a better way to load the mapping file?

     

    Thaks in advance,

    ~ck

     

     

     

     

    Friday, November 14, 2008 7:26 PM