管理具有相同表名的两个实体框架模型

问题描述 投票:0回答:1

使用数据库优先方法,我已将两个模式导入到我的项目中(没有将连接信息保存到 app.config 文件中)到两个不同的文件夹中:

AModel/AModel.edmx
BModel/BModel.edmx
。在这两个模式中,只有两个表具有相同的名称(但列名称不同)。

我编写了这个单例类来管理连接:

using System;
using System.Data.OleDb;
using SynchronizeDatabase.Properties;
using System.Data.Entity.Core.EntityClient;

using AEntities = SynchronizeDatabase.AModel.Entities;
using BEntities = SynchronizeDatabase.BModel.Entities;

namespace SynchronizeDatabase.Tools
{
    class DBInstance
    {
        private static volatile DBInstance _instance;
        private static readonly object _padLock = new object();

        private readonly OleDbConnection _accessConnection;
        private readonly Lazy<AEntities> _lazyContextA;
        private readonly Lazy<BEntities> _lazyContextB;

        private DBInstance()
        {
            _accessConnection = new OleDbConnection(Settings.Default.ACCESS_PATH);
            _lazyContextA = new Lazy<AEntities>(() => new AEntities(BuildConnectionString(Settings.Default.A_DB_NAME)));
            _lazyContextB = new Lazy<BEntities>(() => new BEntities(BuildConnectionString(Settings.Default.B_DB_NAME)));
        }

        public static DBInstance Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_padLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new DBInstance();
                        }
                    }
                }
                return _instance;
            }
        }

        public OleDbConnection AccessConnection => _accessConnection;
        public AEntities AContext => _lazyContextA.Value;
        public BEntities BContext => _lazyContextB.Value;

        private string BuildConnectionString(string name)
        {
            Settings settings = Settings.Default;
            string provider = $"data source={settings.DB_HOST};" +
                              $"initial catalog={name};" +
                              $"persist security info=True;" +
                              $"{(!string.IsNullOrEmpty(settings.DB_USER) ? $"user id={settings.DB_USER};password={settings.DB_PASSWORD};" : "Integrated Security=True;")}";

            string metadata = $"res://*/{name}Model.{name}Model.csdl|res://*/{name}Model.{name}Model.ssdl|res://*/{name}Model.{name}Model.msl";


            return new EntityConnectionStringBuilder
            {
                Provider = "System.Data.SqlClient",
                ProviderConnectionString = provider + "MultipleActiveResultSets=True;App=EntityFramework;",
                Metadata = metadata,
            }.ToString();
        }
    }
}

当我尝试应用

Count()
linq 方法将行数计入一个表时,我遇到了以下异常:

System.Data.Entity.Core.MetadataException:“指定的架构无效。 错误:CLR 类型到 EDM 类型的映射不明确 因为多个 CLR 类型与 EDM 类型“TableA”匹配。发现 CLR 类型 早些时候

我将

.edmx
文件从
<EntityType Name="PlasticComponent">
更改为
<EntityType Name="PlasticComponent" customannotation:ClrType="CaleffiCodeModel.PlasticComponent">
但问题仍然存在

c# sql-server entity-framework
1个回答
0
投票

我通过更改导入方法解决了问题。

我第一次将模型导入为

Entity Framework Designer from Database

现在,将模型导入为

Code First from Database
,即使有同名的表,两个模型也可以共存。

© www.soinside.com 2019 - 2024. All rights reserved.