none
EF 4.3 Not thread safe? RRS feed

  • Question

  • // Class Library Code
    // Ref .NET version 4.0
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    namespace Test.Model
    {
        public class Data
        {
            public Int32 Id { get; set; }
            public Int32 Value { get; set; }
        }
    
        public class DataDbContext : DbContext
        {
            public DbSet<Data> DataListing { get; set; }
        }
    }
    // Main Program Code
    // Ref .NET version 4.0
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using Test.Model
    
    namespace Test
    {
        public class Program
        {
            static void Main(string[] args)
            {
                DataDbContext db = new DataDbContext();
    
                // ERROR Description
                //  - No exception is thrown.
                //  - db.Data = null, due to "The function evaluation requires all threads to run" at base.Database.Connection.
    
                db.DataListing.Add(
                    new Data()
                    {
                        Value = 999;
                    }
                );
            }
        }
    }

    I'm creating a Class Library using EF 4.3, which will be used in a Console Program.

    But i'm facing some error, and i suspect that its a cross thread problem. Any idea how can i resolve this?

    Many thanks!

    Thursday, February 23, 2012 9:53 AM

Answers

  • Hi j3r,

    Welcome to MSDN Forum.

    I have used EF4.3 to repro the issue, after adding the entity to the context and calling SaveChanges(), everything works well, no exception was thrown. I query out the entity and print out it's value property, below is the screen shot of the entity's value property.

    My Class Library is as same as yours. In the Main method, I only add a SaveChanges method to create the database, the database has been created perfectly and everything works well.

    Best Regards


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

    • Marked as answer by j3r Monday, February 27, 2012 2:34 AM
    Monday, February 27, 2012 2:34 AM
    Moderator

All replies

  • Hi j3r,

    Welcome to MSDN Forum.

    I have used EF4.3 to repro the issue, after adding the entity to the context and calling SaveChanges(), everything works well, no exception was thrown. I query out the entity and print out it's value property, below is the screen shot of the entity's value property.

    My Class Library is as same as yours. In the Main method, I only add a SaveChanges method to create the database, the database has been created perfectly and everything works well.

    Best Regards


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

    • Marked as answer by j3r Monday, February 27, 2012 2:34 AM
    Monday, February 27, 2012 2:34 AM
    Moderator
  • I only create an instance of the DbContext without adding any entity and run it. An exception was thrown due to the "Lazy" instantiation.

    After i tried adding data and saved it, everything works fine.

    Thanks for the help!!!

    Cheers!

    Monday, February 27, 2012 2:37 AM
  • Hi j3r,

    I'm glad to hear that you have solved the issue. In Entity Framework, lazy loading is default, the query statement will not hit the database, it only declare a query variable.

    Best Regards


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

    Monday, February 27, 2012 2:39 AM
    Moderator
  • Sorry can i reuse this thread?

    I wanted to make one of my fields to be unique. How can i accomplish that using Data Annotations.

    I want to have Id has PK and another field to be unique, different for every entry.

    For example, a class, User has userId and a userName.

    userId is the PK

    userName should be different for every entry.

    Is it possible to do it using Data Annotations? Or do i have to do my old validation?

    public class MyClass
    {
        [Key]
        public int Id { get; set; }
    
        // How do i make this field unique?
        public int SomethingSpecial { get; set; }
    }

    Thanks in advance!

    Cheers!

    Monday, February 27, 2012 2:47 AM
  • Hi j3r,

    Unitl now, there's no way to add unique constaint with a property in code-first, but you can add it through modify XML of the EDMX file if you use database first or model first. Here's a link can help you to do that. Please refer to it.

    Unique Constraints in the Entity Framework

    If you have new problems, please open a new thread to ask, this is can help other members to search more accurate. Thanks for understanding.

    Best Regards


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

    Monday, February 27, 2012 2:58 AM
    Moderator