EF6 Not Working After Column Change [Invalid Column] - c#

I have several classes in my real code but for this problem I will focus on the two at hand. I have a List class which contains a Name and Items. There is also a Group class which has a name and Items. The relationship used to be List -> Group (many) -> Items. The default for our data seemed to trend more toward List -> Items (many) -> Group where an item may, or may not, be grouped. To accomplish this I simply removed the ListId and navigational item from the Group class. I am able to load the Items just fine but whenever I try to access the Groups DbSet it gives me the following error:
Invalid column name 'List_ListId'.
Here is the code for the classes and the DbContext. I am using EF6 (not core). I have tried recreating the Group class, restarting Visual Studio, restarting SQL Server, and wiping the publish directory to make sure it is clean. Help?
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
namespace MyProject.Database
{
[Table("dbo.Lists")]
public class List
{
[Key]
public int ListId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public bool SortAsNumeric { get; set; }
public virtual List<ListItem> Items { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
namespace MyProject.Database
{
[Table("dbo.ListGroups")]
public class ListGroup
{
[Key]
public int ListGroupId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public int OrderSequence { get; set; }
[NotMapped]
public List<ListItem> Items { get; set; }
public ListGroup()
{
this.Items = new List<ListItem>();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace MyProject.Database
{
[Table("dbo.ListItems")]
public class ListItem
{
[Key]
public int ListItemId { get; set; }
[Required]
public string Label { get; set; }
[Required]
public string Value { get; set; }
public string Subtext { get; set; }
public string Icon { get; set; }
public string Thumbnail { get; set; }
public int OrderSequence { get; set; }
[NotMapped]
public bool Selected { get; set; }
public int ListId { get; set; }
public virtual List List { get; set; }
public int? ListGroupId { get; set; }
public virtual ListGroup Group { get; set; }
public virtual List<ListItemProperty> Properties { get; set; }
public ListItem()
{
this.OrderSequence = 1;
this.Selected = false;
this.Properties = new List<ListItemProperty>();
}
}
}
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
namespace MyProject.Database
{
public class ListContext : DbContext
{
public ListContext() : base("MyConnectionString") {}
public virtual DbSet<List> Lists { get; set; }
public virtual DbSet<ListItem> Items { get; set; }
public virtual DbSet<ListGroup> Groups { get; set; }
public virtual DbSet<ListItemProperty> Properties { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<List>()
.HasMany(l => l.Items)
.WithRequired(li => li.List)
.HasForeignKey<int>(li => li.ListId);
modelBuilder.Entity<ListItem>()
.HasMany(li => li.Properties)
.WithRequired(p => p.Item)
.HasForeignKey<int>(p => p.ListItemId);
}
}
}
NOTE: The following code is working. The problem comes when I attempt to access this.Groups in the context.
List MyList = Lists
.Where(x => x.Name == ListName)
.Include(x => x.Items.Select(i => i.Properties))
.SingleOrDefault();

Related

Issue with SelectList - NullReferenceException

I have set up a SelectList to select an album when creating a track for the chinook database. Following Microsoft docs instruction, for some reason (that I can't see as I'm still trying to learn) it doesn't like what I am putting in my view on razor pages. The code for the walk through is below.
Entities:
public class Album
{
public int AlbumId { get; set; }
public string Title { get; set; }
public int ArtistId { get; set; }
//related entities
public virtual ICollection<Track> Tracks { get; set; }
public virtual Artist Artist { get; set; }
}
public class Track
{
public int TrackId { get; set; }
public string Name { get; set; }
public int AlbumId { get; set; }
public string Composer { get; set; }
public int Milliseconds { get; set; }
public int Bytes { get; set; }
public double UnitPrice { get; set; }
public int MediaTypeId { get; set; }
public int GenreId { get; set; }
//related entities
public virtual Album Album { get; set; }
}
Creating the model for SelectedList:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using UWS.Project;
namespace Project.Pages.Albums
{
public class AlbumPageModel : PageModel
{
public SelectList AlbumNameSL { get; set; }
public void PopulateAlbumsDropDownList(Chinook db,
object selectedAlbum = null)
{
var albumsQuery = from a in db.Albums
orderby a.Title // Sort by name.
select a;
AlbumNameSL = new SelectList(albumsQuery,
"AlbumId", "Title", selectedAlbum);
}
}
}
Now my Model for creating the track:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using UWS.Project;
namespace Project.Pages.Albums
{
public class TrackCreateModel : AlbumPageModel
{
private Chinook db;
public TrackCreateModel(Chinook injectedContext)
{
db = injectedContext;
}
public IActionResult OnGet(int? id)
{
PopulateAlbumsDropDownList(db);
return Page();
}
[BindProperty]
public Track Track { get; set; }
public async Task<IActionResult> OnPostAsync()
{
var emptyTrack = new Track();
if (await TryUpdateModelAsync<Track>(
emptyTrack,
"track", // Prefix for form value.
s => s.AlbumId, s => s.Name, s => s.Composer, s => s.Milliseconds, s => s.Bytes, s
=> s.MediaTypeId, s => s.GenreId, s => s.UnitPrice))
{
db.Tracks.Add(emptyTrack);
await db.SaveChangesAsync();
return RedirectToPage("./Index");
}
// Select AlbumId if TryUpdateModelAsync fails.
PopulateAlbumsDropDownList(db, emptyTrack.AlbumId);
return Page();
}
}
}
Trying to implement the SelectList in my view:
Being new to this I cannot for the life of me see the issue. I have looked at previous issues on here with SelectedList's but none relate to my issue. Any help would be appreciated.
Do not enter selectedValue in the PopulateAlbumsDropDownList method in AlbumPageModel
Currently you either leave it blank or provide an id, but that's not going to work anyway

Relationship (database first) in Entity Framework

I created a database, then applied dataase-first. Then it automatically imported the database to VS. Please tell me, when database-first automatically indicates relationship? Probably not, my data is not being imported. Could you tell me how to establish connections correctly? I read about the fluent api and about the fact that you can specify keys and properties directly in the table classes (And when is it better to do through fluent, and when to specify directly?)
My 1st table
namespace WcfRestFullService.Model
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
[DataContract]
public partial class customer
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public customer()
{
this.dishesrankings = new HashSet<dishesranking>();
this.orders = new HashSet<order>();
}
[DataMember]
public int Id_Cus { get; set; }
[DataMember]
public string FirstName_Cus { get; set; }
[DataMember]
public string LastName_Cus { get; set; }
[DataMember]
public int PhoneNum_Cus { get; set; }
[DataMember]
public string Email_Cus { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<dishesranking> dishesrankings { get; set; }
public virtual customerpreference customerpreference { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<order> orders { get; set; }
}
}
My 2nd table
namespace WcfRestFullService.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
[DataContract]
public partial class customerpreference
{
[DataMember]
public int Id_Cus { get; set; }
[DataMember]
public int Id_Res { get; set; }
[DataMember]
public string Name_Dis { get; set; }
[DataMember]
public int Id_Type { get; set; }
public virtual customer customer { get; set; }
public virtual order order { get; set; }
public virtual type_dishes type_dishes { get; set; }
}
}
MySQLEntities
namespace WcfRestFullService.Model
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MySQLEntities : DbContext
{
public MySQLEntities()
: base("name=MySQLEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<customer>()
.HasMany(c => c.customerpreference)
.WithOptional(o => o.Customer);
//throw new UnintentionalCodeFirstException();//here problem
}
public virtual DbSet<customer> customers { get; set; }
public virtual DbSet<customerpreference> customerpreferences { get; set; }
public virtual DbSet<dish> dishes { get; set; }
public virtual DbSet<dishesranking> dishesrankings { get; set; }
public virtual DbSet<ingridient> ingridients { get; set; }
public virtual DbSet<order> orders { get; set; }
public virtual DbSet<restaraunt> restaraunts { get; set; }
public virtual DbSet<type_dishes> type_dishes { get; set; }
public object Parameters { get; internal set; }
}
}
Here I create data(Id_Cus) but it doesn't import in 2nd table
public void InsertCustomer(customer customerDataContract)
{
//MySQLEntities Cust = new MySQLEntities();
customer cust = new customer();
{
cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
cust.FirstName_Cus = customerDataContract.FirstName_Cus;
cust.LastName_Cus = customerDataContract.LastName_Cus;
cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);
cust.Email_Cus = customerDataContract.Email_Cus;
};
dc.customers.Add(cust);
customerpreference custPref = new customerpreference()
{
Id_Cus = customerDataContract.Id_Cus,
Id_Res = 0, // some value
Name_Dis = null, // some value
Id_Type = 0 // some value
};
dc.customerpreferences.Add(custPref);
dc.SaveChanges();
int k = Convert.ToInt32(cust.Id_Cus);
customer custFromDb =(from n in dc.customers
where n.Id_Cus == k
select n).Include(c => c.customerpreference).First();
}
perhaps problem in
cust = (from n in dc.customers
where n.Id_Cus == k
select n).Include(c =>c.customerpreference).ToList().First();
dc.customers.Add(cust);
dc.SaveChanges();
Yes it does automatically model foreign keys. You can see that it has done so in your model because there are navigation properties such as dishesrankings in your Customer class.
We had a database first project. We would update it by changing the database using dbup and then updating the model from the database. This way you ensure consistency between the model and the database.
DbUp: https://dbup.github.io/
DbUp is a tool to run scripts to make changes to the database that allows versioning and rollback and it is very useful if you're using database first.

How to fix Entity Framework Eager loading coming out blank or null

I set up my database with two tables Reports and Expenses
Joined on ReportId
I can not seem for the life of me get the expenses to come out without.
Spent a few days in tutorials.
using ExpenseAPI.Models;
using Microsoft.EntityFrameworkCore;
namespace ExpenseAPI.Data
{
public class ReportsDbContext : DbContext
{
public ReportsDbContext(DbContextOptions<ReportsDbContext> options):base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
System.Console.Out.WriteLine("on model create");
/* Expense composite key */
modelBuilder.Entity<Expense>()
.HasKey(c => new { c.ReportId, c.ExpenseId });
/* Reports to Expenses one to many */
modelBuilder.Entity<Reports>()
.HasMany(e => e.Expenses)
.WithOne(r => r.Report)
.HasPrincipalKey(e => e.ReportId);
}
public DbSet<Reports> Reports { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace ExpenseAPI.Models
{
public class Reports
{
public Reports()
{
Expenses = new List<Expense>();
}
[Key]
public int ReportId { get; set; }
[Required]
public string ReportName { get; set; }
public ICollection<Expense> Expenses { get; set; }
}
}
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ExpenseAPI.Models
{
public class Expense
{
[Key, Column(Order = 0)]
public int ReportId { get; set; }
[Key, Column(Order = 2)]
public int ExpenseId { get; set; }
[Required]
public DateTime Date { get; set; }
[Required]
public decimal Amount { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Payee { get; set; }
public Reports Report { get; set; }
}
}
using System.Linq;
using ExpenseAPI.Data;
using ExpenseAPI.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace ExpenseAPI.Controllers
{
[Route("api/reports")]
[ApiController]
public class ReportsController : ControllerBase
{
ReportsDbContext reportsDbContext;
public ReportsController(ReportsDbContext _reportsDbContex)
{
reportsDbContext = _reportsDbContex;
}
// GET: api/Reports
[HttpGet]
public IActionResult Get()
{
var result = reportsDbContext.Reports;
result.Include("Expenses");
return Ok(result);
// ISSUE IS HERE expenses is empty
}
}
}
Result:
[{"reportId":1,"reportName":"August Expense 2019-08","expenses":[]}]
Expected:
[{"reportId":1,"reportName":"August Expense 2019-08","expenses":[
{Expense Data Here}
]}]
Before someone says make sure there is data in expenses, there is a expense record that does have the ReportId of 1
Check the debug console.
Would have noticed an error
//If using ASP.NET Core MVC, add this to the ConfigureServices method of your startup.cs file:
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore
);

Many to many relationship returns 0 element collection using entity framework 6

I have a probably simple question, I am trying to create many to many relationships using entity framework and fluent api, and my problem is that when i try to do any query or view a object in debug it has always 0 items.
I am using junction table that looks like:
So relations exists, to be sure ive checked:
select candidate.firstname, skillset.name
from candidate
join candidate_skillset on candidate.id = candidate_skillset.candidate_id
join skillset on candidate_skillset.skillset_id = skillset.id
and joined results are displayed.
Now my context looks like:
public class CatalogContexct : DbContext
{
public DbSet<Candidate> Candidates { get; set; }
public DbSet<SkillSet> SkillSets { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
.Map(m =>
{
m.ToTable("candidate_skillset");
m.MapLeftKey("candidate_id");
m.MapRightKey("skillset_id");
});
modelBuilder.Entity<SkillSet>().ToTable("skillset");
modelBuilder.Entity<Candidate>().ToTable("candidate");
}
}
My left side model candidates:
[Table("candidate")]
public class Candidate
{
public Candidate()
{
this.SkillSets = new HashSet<SkillSet>();
}
[Key]
public int id { get; set; }
[Column("firstname")]
public string Firstname { get; set; }
public int? commendation_id { get; set; }
[ForeignKey("commendation_id")]
public Commendation commendation { get; set; }
public ICollection<SkillSet> SkillSets { get; set; }
}
And my rightside model skillset:
[Table("skillset")]
public class SkillSet : SimpleDictionary
{
public SkillSet()
{
this.Candidates = new HashSet<Candidate>();
}
public virtual ICollection<Candidate> Candidates { get; set; }
}
and that model has a parent class:
public class SimpleDictionary
{
[Key]
public int id { get; set; }
[Column("name")]
public string Name { get; set; }
}
So all should work but when I do for example:
var ca = this._catalog.Candidates
.Include("SkillSets").Include("commendation").
FirstOrDefault(x => x.SkillSets.Any());
Result is null, also when I view object on debug collection of property skillset allays has 0 elements, any idea what could be wrong with it?
I tried this with same structure mentioned here in you question and tried locally . And I am able to get the data with this code . Please try this and let me know if this helps . I just omitted commendation table for simplicity .
var context = new SampleDbContext();
var candidates = context.Candidates
.Include("SkillSets").ToList();
foreach (var candidate in candidates)
{
foreach (var sk in candidate.SkillSets.Where( s1 => s1.Candidates.Count(c=>c.id == candidate.id)>0 ))
{
Console.WriteLine( string.Format(#" Name : {0} Skill :{1}",candidate.Firstname ,sk.Name ) );
}
}
Below is my DbContext and Other Entity Classes
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class SampleDbContext : DbContext
{
public SampleDbContext()
: base("name=SampleDBConnection")
{
this.Configuration.LazyLoadingEnabled = false;
}
public DbSet<Candidate> Candidates { get; set; }
public DbSet<SkillSet> SkillSets { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
.Map(m =>
{
m.ToTable("candidate_skillset");
m.MapLeftKey("candidate_id");
m.MapRightKey("skillset_id");
});
modelBuilder.Entity<SkillSet>().ToTable("skillset");
modelBuilder.Entity<Candidate>().ToTable("candidate");
}
}
[Table("candidate")]
public class Candidate
{
public Candidate()
{
this.SkillSets = new HashSet<SkillSet>();
}
[Key]
public int id { get; set; }
[Column("firstname")]
public string Firstname { get; set; }
public int? commendation_id { get; set; }
//[ForeignKey("commendation_id")]
//public Commendation commendation { get; set; }
public ICollection<SkillSet> SkillSets { get; set; }
}
public class SimpleDictionary
{
[Key]
public int id { get; set; }
[Column("name")]
public string Name { get; set; }
}
[Table("skillset")]
public class SkillSet : SimpleDictionary
{
public SkillSet()
{
this.Candidates = new HashSet<Candidate>();
}
public virtual ICollection<Candidate> Candidates { get; set; }
}
}
The output of the query you mentioned and the result of my code both matched I hope this is that you wanted .

Get and set items in a collection

I have two classes: Group and Item.
public class Group
{
public string Name{ get; set; }
public List<Item> ItemList { get; set; }
}
And then Item
public class Item
{
public int ID{ get; set; }
public string Name{ get; set; }
public string Description{ get; set; }
public Group ItemGroup{ get; set; }
}
Each group show have a set of items.
The following code is meant to get the list of items of a particular group.
public IEnumerable<Item> GetItemByGroup(string group)
{
return repository.GetAllItems().Where
(p =>p.ItemGroup.Name.Equals(group));
}
Full controller code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TestService.Models;
using System.IO;
namespace TestService.Controllers
{
public class ItemsController : ApiController
{
static readonly IItemRepository repository = new ItemRepository();
public IEnumerable<Item> GetAllItems()
{
return repository.GetAllItems();
}
public IEnumerable<Item> GetItemsByGroup(string group)
{
return repository.GetAllItems().Where
(p => p.Category.Name.Equals(group));
}
}
}
When I run this I get the error: No type was found that matches the controller named 'item'. When I called it using localhost:1234/api/item?group=group1.
How do I get the list of items in a particular category specified by a string?
Your URL is incorrect. Try .../api/items ... instead of .../api/item...

Categories

Resources