C#序列不包含任何元素 - 加载还是添加?

时间:2016-11-11 18:52:18

标签: c# wpf entity-framework

我在C#WPF应用程序中收到以下错误:

StackTrace  "   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.<>c__DisplayClass4.<Configure>b__3(Tuple`2 pm)
   at System.Data.Entity.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
   at System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride)
   at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest)
   at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
   at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator()
   at System.Data.Entity.QueryableExtensions.Load(IQueryable source)
   at Insurance_MidAm_Interface.MainWindow.Window_Loaded(Object sender, RoutedEventArgs e) in c:\\Users\\Khandokar\\Documents\\Visual Studio 2013\\Projects\\Insurance MidAm Interface\\Insurance MidAm Interface\\MainWindow.xaml.cs:line 80"   string

这是抛出异常的代码:

     System.Windows.Data.CollectionViewSource employeeViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeeViewSource")));
   try { dbContext.employees.Load();  //// Line 80    

        }
        catch (Exception EX)
        {
            Console.Write(EX.InnerException);
        }
employeeViewSource.Source = dbContext.employees.ToList();

    }

我几乎尝试了所有我能想到的东西。我已经将测试数据添加到数据库中,甚至放置了employeeViewSource.Source = dbContext.employees.ToList()和dbContext.employees.Load();在Try / Catch块中。每当我执行后者时,应用程序将加载,但每当我尝试添加实体时,我都会得到相同的错误。我环顾四周,大多数解决方案都没有调用Single(而是使用SingleOrDefault)。但是,我没有明确地打电话给&#34; Single&#34; (虽然从Stack Trace看来,它显然已经被调用了。)

问题可能是什么?

非常感谢。

员工模型

public partial class employee
{
    public employee()
    {
        beneficiaries = new HashSet<beneficiary>();
        children = new HashSet<child>();
        reports = new HashSet<report>();
        spouses = new HashSet<spouse>();
        visions = new HashSet<vision>();
    }

    [Key]
    [Column(Order = 0)]
    [StringLength(12)]
    public string id { get; set; }

    [Key]
    [Column(Order = 1)]
    [StringLength(4)]
    public string year { get; set; }

    [Required]
    [StringLength(30)]
    public string first { get; set; }

    [Required]
    [StringLength(30)]
    public string last { get; set; }

    [Required]
    [StringLength(10)]
    public string gender { get; set; }

    [Required]
    [StringLength(50)]
    public string email { get; set; }

    [Column(TypeName = "text")]
    [Required]
    public string address { get; set; }

    [Required]
    [StringLength(50)]
    public string location { get; set; }

    public DateTime birth { get; set; }

    private string _displayDate;
    public string displayDate
    {
        get
        {
            _displayDate = this.birth.ToShortDateString();
            return _displayDate;
        }
    }


    private string displaySSN;
    public string SSN
    {
        get
        {
            ssnDecrypter decrypter = new ssnDecrypter();
            decrypter.cryptString = this.ssn;
            byte[] key = decrypter.hexToBin(MainWindow.KEY);
            byte[] ssn = decrypter.hexToBin(decrypter.getSSN());
            byte[] iv = decrypter.hexToBin(decrypter.getIV());
            displaySSN = decrypter.decrypter(ssn, key, iv);
            return displaySSN;


        }
    }


    [StringLength(50)]
    public string decision { get; set; }

    [Required]
    [StringLength(10)]
    public string dependents { get; set; }

    [Required]
    [StringLength(30)]
    public string vision { get; set; }

    [Required]
    public string ssn { get; set; }

    public bool filed { get; set; }


    public virtual ICollection<beneficiary> beneficiaries { get; set; }

    public virtual ICollection<child> children { get; set; }

    public virtual ICollection<report> reports { get; set; }

    public virtual ICollection<spouse> spouses { get; set; }

    public virtual ICollection<vision> visions { get; set; }
}
}

insuranceModel(dbContext)

public partial class insuranceModel : DbContext
{
    public insuranceModel()
        : base("name=insuranceModel2")
    {
    }

    public virtual DbSet<action> actions { get; set; }
    public virtual DbSet<beneficiary> beneficiaries { get; set; }
    public virtual DbSet<child> children { get; set; }
    public virtual DbSet<employee> employees { get; set; }
    public virtual DbSet<report> reports { get; set; }
    public virtual DbSet<spouse> spouses { get; set; }
    public virtual DbSet<vision> visions { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<action>()
            .HasMany(e => e.reports)
            .WithRequired(e => e.action)
            .HasForeignKey(e => e.decision)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<beneficiary>()
            .Property(e => e.year)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<beneficiary>()
            .Property(e => e.ssn)
            .IsUnicode(false);

        modelBuilder.Entity<beneficiary>()
            .Property(e => e.name)
            .IsFixedLength();

        modelBuilder.Entity<beneficiary>()
            .Property(e => e.address)
            .IsUnicode(false);

        modelBuilder.Entity<child>()
            .Property(e => e.year)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<child>()
            .Property(e => e.ssn)
            .IsUnicode(false);

        modelBuilder.Entity<employee>()
            .Property(e => e.year)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<employee>()
            .Property(e => e.address)
            .IsUnicode(false);

        modelBuilder.Entity<employee>()
            .Property(e => e.location)
            .IsFixedLength();

        modelBuilder.Entity<employee>()
            .Property(e => e.ssn)
            .IsUnicode(false);

        modelBuilder.Entity<employee>()
            .HasMany(e => e.beneficiaries)
            .WithRequired(e => e.employee)
            .HasForeignKey(e => new { e.empId, e.year })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<employee>()
            .HasMany(e => e.children)
            .WithRequired(e => e.employee)
            .HasForeignKey(e => new { e.empId, e.year })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<employee>()
            .HasMany(e => e.reports)
            .WithRequired(e => e.employee)
            .HasForeignKey(e => new { e.empId, e.year })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<employee>()
            .HasMany(e => e.spouses)
            .WithRequired(e => e.employee)
            .HasForeignKey(e => new { e.empId, e.year })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<employee>()
            .HasMany(e => e.visions)
            .WithRequired(e => e.employee)
            .HasForeignKey(e => new { e.empId, e.year })
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<report>()
            .Property(e => e.year)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<spouse>()
            .Property(e => e.year)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<spouse>()
            .Property(e => e.ssn)
            .IsUnicode(false);

        modelBuilder.Entity<spouse>()
            .Property(e => e.name)
            .IsUnicode(false);

        modelBuilder.Entity<spouse>()
            .Property(e => e.address)
            .IsUnicode(false);

        modelBuilder.Entity<spouse>()
            .Property(e => e.gender)
            .IsUnicode(false);

        modelBuilder.Entity<spouse>()
            .Property(e => e.isEmployed)
            .IsUnicode(false);

        modelBuilder.Entity<vision>()
            .Property(e => e.year)
            .IsFixedLength()
            .IsUnicode(false);

        modelBuilder.Entity<vision>()
            .Property(e => e.ssn)
            .IsUnicode(false);

        modelBuilder.Entity<vision>()
            .Property(e => e.name)
            .IsUnicode(false);

        modelBuilder.Entity<vision>()
            .Property(e => e.gender)
            .IsUnicode(false);
    }
}
}

0 个答案:

没有答案