none
Problem with Context RRS feed

  • Question

  • Hello guys;

    I'm creating a project using Entity Framework 4.1 and I'd like to create a Repository for persistence.

    I have this structure:

    //Context class
    public class Context : DbContext
    { 
        public Context() : base(@"Data Source=.\SQLEXPRESS;Initial Catalog=GestaoTreinamento;Integrated Security=True") { }
    
        public DbSet<Questionario> Questionario { get; set; }
    }
    
    //My interface
    public interface IRepository<T> where T : IEntity
    {
        void Delete(T t);
        void Add(T t);
        void Edit(T t);
        T GetById(int id);
        List<T> GetAll();
        void Commit();
    }
    
    //My Repository
    public class Repository<T> : IRepository<T> where T : IEntity
    {
        public Context contexto;
    
        public Repository(Context context)
        {
            this.contexto = context;
        }
    
        public void Add(T t)
        {
            this.contexto.Set(typeof(T)).Add(t);
            this.contexto.SaveChanges();            
        }
    }
    
    //That is my Entity Questionario
    [Table("QUESTIONARIO")]
    public class Questionario : IEntity
    {
        [Key]
        [Column("QUES_ID_QUESTIONARIO")]
        public int IdQuestionario { get; set; }
    
        [Column("QUES_NM_QUESTIONARIO")]
        [StringLength(255)]
        public String NomeQuestionario { get; set; }
    
        [Column("QUES_IN_ATIVO")]    
        public Boolean IndicadorAtivo { get; set; }
    }

    When I try to persist an Entity I receive that error message:

    "The argument 'name' cannot be null, empty or contain only white space."

    What's happening? I search on the Internet but I don't found any solution for my problem!

    Any idea?


    Deise Vicentin
    "Eu não procuro saber as respostas, procuro compreender as perguntas." Confúcio

    Sunday, June 24, 2012 2:43 PM

Answers

  • Hi Deise,

    As per my suggestion, you need to use the parameterless constructor for the DbContext. Then, your app.config key for the connection string must be the same name as your class that inherits from DbContext, and Entity Framework is clever enough to figure out to use that connection string based on the name match. You don't need to define the connection string anywhere else in your code.

    I don't know which version this bug was in, but it's probably best to follow Allen's advice and upgrade to the latest version of EF (4.3.1) if the bug has been fixed, then you can define your connection string any way you want to.

    Regards,


    Justin Fortmann

    Monday, June 25, 2012 7:48 PM

All replies

  • Hi Deise

    Try changing...

        public void Add(T t)
        {
            this.contexto.Set(typeof(T)).Add(t);
            this.contexto.SaveChanges();            
        }
    


    to...

        public void Add(T t)
        {
            this.contexto.Set<T>().Add(t);
            this.contexto.SaveChanges();            
        }
    

    Regards,

    Justin Fortmann

    Sunday, June 24, 2012 4:19 PM
  • Hi Deise

    Try changing...

        public void Add(T t)
        {
            this.contexto.Set(typeof(T)).Add(t);
            this.contexto.SaveChanges();            
        }


    to...

        public void Add(T t)
        {
            this.contexto.Set<T>().Add(t);
            this.contexto.SaveChanges();            
        }

    Regards,

    Justin Fortmann

    Hi Justin;

    I replaced but doesn't work too. I received the same error message.

    Thank you!


    Deise Vicentin
    "Eu não procuro saber as respostas, procuro compreender as perguntas." Confúcio

    Sunday, June 24, 2012 5:11 PM
  • I believe this may be a bug when defining SQL connection strings in the DbContext constructor.

    Try using the default no parameter constructor for the DbContext, and then define the SQL connection string in your web.config/app.config.

    ie.

    <connectionStrings>
    <add name="Context" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=GestaoTreinamento;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>


    Justin Fortmann

    Sunday, June 24, 2012 7:38 PM
  • Hi Deise Vicentin,

    Welcome to MSDN Forum.

    I suggest you to upgrade Entity Framework. I have created a test project and use the code you posted, everything wokrs well, no error appears, and the database is created. My EF version is 4.3.1.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, June 25, 2012 9:44 AM
    Moderator
  • Hi;

    I think this is a problem in establishment of connection with the database, but I don't know what is the problem!

    I changed my Context class. Take a look:

    //Context Class
    public Context(string connectionString) : base(connectionString) {  }
    
    //I created a ObjectBO class to access the Repository
    public class ObjectBO<T> where T : class, new()
    {
        protected Context db = new Context(System.Configuration.ConfigurationSettings.AppSettings["StringConexao"].ToString());
    
        public Repository<T> rep;
    
        public ObjectBO()
        {
            rep = new Repository<T>(db);
        }
    
        public void Add(T t)
        {            
            rep.Add(t);
        }
    
    }

    That is my app.config:

    <appSettings>
        <add key="StringConexao" value="Data Source=.\SQLEXPRESS2008;Initial Catalog=GestaoTreinamento;Integrated Security=True;"/>
    </appSettings>

    I tried put in the constructor of Context something like that:

     Database.Connection.Open();
     Database.DefaultConnectionFactory.CreateConnection(connectionString);


    But that doesn't work. I receive the same error message.

    While I'm debugging, I saw the State of Connection of Context is closed. But when I try to Open the connection doesn't work the same way.

    I'm going crazy, because I did a project like that and it work very well. 

    I'll try to change the version of EF.

    Thanks!


    Deise Vicentin
    "Eu não procuro saber as respostas, procuro compreender as perguntas." Confúcio

    Monday, June 25, 2012 1:48 PM
  • Hi Deise,

    As per my suggestion, you need to use the parameterless constructor for the DbContext. Then, your app.config key for the connection string must be the same name as your class that inherits from DbContext, and Entity Framework is clever enough to figure out to use that connection string based on the name match. You don't need to define the connection string anywhere else in your code.

    I don't know which version this bug was in, but it's probably best to follow Allen's advice and upgrade to the latest version of EF (4.3.1) if the bug has been fixed, then you can define your connection string any way you want to.

    Regards,


    Justin Fortmann

    Monday, June 25, 2012 7:48 PM
  • Hi;

    I created the .edmx in my application to see if that work. And worked!

    I didn't find no link for download the latest version of EF and now I don't have much time for that. I'll try to create a connection in the future and when I get I'll post here.

    Thank you for your answers!


    Deise Vicentin
    "Eu não procuro saber as respostas, procuro compreender as perguntas." Confúcio

    Thursday, June 28, 2012 12:29 PM
  • Hi Deise Vicentin,

    Please refer to this link, the latest version of Entity Framework is available on NuGet.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 29, 2012 5:50 AM
    Moderator
  • Hi Deise Vicentin,

    Please refer to this link, the latest version of Entity Framework is available on NuGet.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thank you Allen;

    I'll download this version later.

    Best Regards.


    Deise Vicentin
    "Eu não procuro saber as respostas, procuro compreender as perguntas." Confúcio

    Friday, June 29, 2012 12:04 PM