开发者

Trying to connect to a database with ObjectContext, but fails

开发者 https://www.devze.com 2023-03-24 09:13 出处:网络
I\'m playing around with POCOs in EF, working from a tiny basic setup to something more advanced. No interfaces of generics, just an easy point of entry.

I'm playing around with POCOs in EF, working from a tiny basic setup to something more advanced. No interfaces of generics, just an easy point of entry. First of all I created an .edmx file, containing just one entity: Person, with 3 attributes. Id, FirstName and LastName.

With this I generated the database and added a few records into it manually. This database is called 'PocoTest' and the connection bit in my App.Config looks like:

<add name="PocoTestContainer" connectionString="metadata=res://*/PocoTest.csdl|res://*/PocoTest.ssdl|res://*/PocoTest.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=SEBASTIAAN-PC\SQLEXPRESS;Initial Catalog=PocoTest;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

Firstly i created an Entity:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Now I created a Context, inheriting from the ObjectContext:

public class PocoTestContext : ObjectContext
{
    private IObjectSet<Person> persons;

    public PocoTestContext()
        : base("name=PocoTestContainer", "PocoTestContainer")
    {
        ContextOptions.LazyLoadingEnabled = true;
        persons = CreateObjectSet<Person>();           
    }

    public IObjectSet<Person> Persons
    {
        get
        {
            return persons;
       开发者_JAVA百科 }
    }
}

Nothing fancy here. Next thing is a repository:

public class PersonRepository
{
    PocoTestContext context;

    public PersonRepository()
    {
        context = new PocoTestContext();
    }

    public Person GetById(int id)
    {
        return context.Persons.Where(p => p.Id == id).FirstOrDefault();
    }

    public List<Person> GetAll()
    {
        List<Person> persons = null;

        try
        {
            persons = context.Persons.ToList();
        }
        catch(Exception e)
        {
            Console.WriteLine(e.InnerException);
        }
        return persons;
    }

    public void Add(Person entity)
    {
        context.Persons.AddObject(entity);
    }

    public void Save()
    {
        context.SaveChanges();
    }
}

Now all of this compiles fine, but I can't seem to connect to the database, since i don't get any results returned. When I check the connectionstate in the PocoTestContext constructor the connection isn't established. Now I'm guessing something is wrong with the way I use the connectionstring. I stole this approach from another project in which I did use a generated repository.

Any help would be greatly appreciated!


Connection is not established in the constructor. It is established when it is needed and closed once it is not needed.


I've come up with the solution, the edmx file held a reference to 'PersonSet', I manually changed this to 'Person'. Now I get the expected results.

0

精彩评论

暂无评论...
验证码 换一张
取 消