Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: EF 6 Beta 1 - Exception when using IDataErrorInfo on Base Class

$
0
0
Hi,

I'm using the Nuget beta 1 package of EF, and attempting to use code first to connect to my database. I've encountered an exception that was preventing me from ever hitting the database: whenever I tried to connect, I got an exception stating "Sequence contains no matching elements".

After much head-scratching, I've managed to track the cause down to the implementation of IDataErrorInfo in a base class. Specifically the implementation of this[columnName]. Even adding the [NotMapped] attribute to the property doesn't help the issue.

Strangely, if I remove the base class, and move the implementation of IDataErrorInfo into the Model itself, the code works fine.

Below is sample code that will demonstrate the issue.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = "Data Source=.;Initial Catalog=Test;Integrated Security=true;";

            using (TestContext ctxt = new TestContext(connString))
            {
                //This will throw an exception
                var data = from D in ctxt.MyData
                           select D;

                Console.WriteLine("Model Base: item count is: " + data.Count().ToString());
                Console.ReadKey();
            }
        }
    }

    public class TestContext : DbContext
    {
        public TestContext(string connString)
            :base(connString)
        {

        }

        public DbSet<MyData> MyData { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }

    //Remove the ModelBase inheritance and move the IDataErrorInfo implementation into MyData to resolve the issue.
    public class MyData : ModelBase
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public class ModelBase : IDataErrorInfo
    {
        #region IDataErrorInfo

        [NotMapped]
        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        [NotMapped]
        public string this[string columnName]
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
Create Table MyData
(
    Id int not null IDENTITY,
    Name Varchar(100) Not Null
);

Insert into MyData (Name) Values ('Test 1');
Insert into MyData (Name) Values ('Test 2');
Insert into MyData (Name) Values ('Test 3');

Viewing all articles
Browse latest Browse all 1793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>