EF Code First - 序列包含多个元素。迁移创建MVC4

时间:2014-10-09 07:45:52

标签: entity-framework asp.net-mvc-4

我一直在构建一个只有一个数据库(MSSQL)的软件,但是这已经扩展了,我现在从mysql数据库中提取客户信息。到目前为止,当我试图建立一个两者之间存在多对多关系的模型时,我一直在创建模型,这两个模型之间存在联系。我通过名为workdata.edmx的ADO.net Entity Datamodel链接到mysql数据库 我想要一个活动模型(MSSQL),它包含一个exisitng mysql数据库中的系统类型列表。添加迁移时,我收到错误“序列包含多个匹配元素”。以下是我的罪魁祸首模型;

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using ReturnsMVC.Models;

namespace ReturnsMVC.Models
{
    public class Campaign
    {
        public Campaign()
       {
            SystemTypes = new List<system_type>();
       }

        public int id { get; set; }
        public string nameofcampaign { get; set; }
        public string description { get; set; }
        public virtual ICollection<Interaction> Interactions { get; set; }
        public bool active { get; set; }
        public virtual ICollection<system_type> SystemTypes { get; set; }

    }
}

using ReturnsMVC.Models;

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

    public partial class system_type
    {
        public system_type()
        {
            this.customers = new HashSet<customer>();

        }

        public int id { get; set; }
        public string value { get; set; }

        public virtual ICollection<customer> customers { get; set; }
       public virtual ICollection<Campaign> campaigns { get; set; }

    }
}

//模型构建器数据      modelBuilder.Entity()

           .HasMany(r => r.SystemTypes)
           .WithMany(q=>q.campaigns)
           .Map(m =>
           {
               m.ToTable("CampaignSystemTypes");
               m.MapLeftKey("campaignID");
               m.MapRightKey("systemtypeID");

           });

- 栈跟踪 -

    PM> add-migration campaignsystemtype
Scaffolding migration 'campaignsystemtype'.
System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildCreateTableOperation(String entitySetName, String tableName, String schema, ModelMetadata modelMetadata)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<FindAddedTables>b__31(XElement es)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString)
   at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sequence contains more than one matching element
PM> add-migration campaignsystemtype
Scaffolding migration 'campaignsystemtype'.
System.InvalidOperationException: Sequence contains more than one matching element
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.BuildCreateTableOperation(String entitySetName, String tableName, String schema, ModelMetadata modelMetadata)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.<FindAddedTables>b__31(XElement es)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, String connectionString)
   at System.Data.Entity.Migrations.DbMigrator.Scaffold(String migrationName, String namespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder.Scaffold(String migrationName, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Scaffold(MigrationScaffolder scaffolder)
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Sequence contains more than one matching element

PM&GT;

非常感谢任何帮助或指导。我有一些谷歌搜索的感觉,我需要在我的MSSQL数据库上创建一个视图来处理两者之间的关系。

0 个答案:

没有答案