简体   繁体   中英

From SQL database to model using linq

For my homework I need to get the data with ac# application using Entity Framework out of a SQL database.

The problem is that I have no idea what I am doing wrong.

My class:

public class Organisation
{
    public int Id { get; set; }
    public string Name { get; set; }

    public Organisation(int Id, string Name)
    {
        this.Id = Id;
        this.Name = Name;
    }

    public class OrganisationContext : DbContext
    {
        public DbSet<Organisation> Organisations { get; set; }
    }

    public static Organisation Find(int id) {
        using (var context = new OrganisationContext())
        {
            // Query for all blogs with names starting with B 
            var organisation = from b in context.Organisations
                        where b.Id = id
                        select b;
            return organisation;
        }
    }
}

My user class. I use Identity.

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public string Firstname { get; set; }
    public string Interjunction { get; set; }
    public string Lastname { get; set; }
    public int OrganisationId { get; set; }

    public virtual Organisation Organisation
    {
        get
        {
            return Organisation.Find(OrganisationId);
        }
    }  

    public int Role { get; set; }

    public string DisplayName
    {
        get
        {
            string dspFirstname = string.IsNullOrWhiteSpace(this.Firstname) ? "" : this.Firstname;
            string dspInterjunction = string.IsNullOrWhiteSpace(this.Interjunction) ? "" : this.Interjunction + " ";
            string dspLastname = string.IsNullOrWhiteSpace(this.Lastname) ? "" : this.Lastname;

            return string.Format("{0} {1}{2}", dspFirstname, dspInterjunction, dspLastname);
        }
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

I am searching and trying to understand it for a while but how do I convert a var to an organisation model? Or am I missing an important peace of code?

Ok. In your method you want to return single Organization object:

public static Organisation Find(int id)

But your LINQ query actually returns a collection of objects:

using (var context = new OrganisationContext())
{
    // Query for all blogs with names starting with B 
    var organisation = from b in context.Organisations
                where b.Id = id
                select b;
    return organisation;
}

In this case you are filtering organization by primary key and there is no situation when this query returns more then 1 row. Then you can just call SingleOrDefault() :

var organisation = (from b in context.Organisations
                where b.Id = id
                select b).SingleOrDefault();
return organisation;

Also, you can use Find method from DbSet class:

using (var context = new OrganisationContext())
{
    // Query for all blogs with names starting with B 
    var organisation = context.Organisations.Find(id)
    return organisation;
}

One of the common requirements for entities in EF is parameterless constructor. So, you need to remove existed constroctor for Organization class or add another one:

public Organization() { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM