EFCore some Properties aren't populated - entity-framework-core

Im having the problem that some of my objects aren't populated fully and I can't understand why this happens I tested with the following code:
[HttpGet]
[Route("Artikel/Test")]
public async Task<IActionResult> Test()
{
return Json(await db.Artikel_Meldungen
.Include(m => m.Meldung).ThenInclude(a => a.ErsatzArtikel)
.Where(x=>x.Artikel_MeldungId==1726)
.ToListAsync());
}
[HttpGet]
[Route("Artikel/Meldung")]
public async Task<IActionResult> GetMeldungen()
{
return Json((await db.Artikel_Meldungen
.Include(m => m.Meldung).ThenInclude(a => a.ErsatzArtikel)
.ToListAsync())
.Where(x => x.Artikel_MeldungId == 1726));
}
with the following classes:
[Table("Meldung", Schema = "BDB")]
public class Meldung
{
[Key]
public int Meldung_Id { get; set; }
public string Erfasser { get; set; }
public Artikel ErsatzArtikel { get; set; }
[NotMapped]
public Artikel Parent { get; set; }
public long ErsatzArtNr { get; set; }
[Column(TypeName = "nvarchar(2)")]
public string ErsatzAusfKz { get; set; }
public virtual ICollection<Artikel_Meldung> Artikel_Meldungen { get; set; }
public string Kommentar { get; set; }
public DateTime StartZeitpunkt { get; set; }
public DateTime EndeZeitpunkt { get; set; }
}
[Table("Artikel_Meldung", Schema = "BDB")]
public class Artikel_Meldung
{
[Key]
public int Artikel_MeldungId { get; set; }
public int Meldung_Id { get; set; }
public Meldung Meldung { get; set; }
public Artikel Artikel { get; set; }
public long ArtNr { get; set; }
[Column(TypeName = "nvarchar(2)")]
public string Ausfkz { get; set; }
}
[Table("Artikeldaten_PRE", Schema = "BDB")]
public class Artikel
{
[Key]
public long ArtNr { get; set; }
[Column(TypeName = "nvarchar(2)")]
public string AusfKz { get; set; }
public virtual ICollection<Artikel_Meldung> Artikel_Meldungen { get; set; }
}
As for my understanding both should return the same result. But for some reason the "Artikel/Meldung" endpoint isn't populating the ErsatzArtikel object. Am I doing something wrong or is this a bug?
Regards Martin

Adding AsNoTracking() AsSplitQuery() fixed the issue

Related

Can anyone give me a solution how we will merge the two different repositories i.e.. two tables in a same database we want to use a Linq query

Working code - but it is not getting output what is the wrong in this code:
var result = _favouriteProjectRepository.Entity()
.Where(x => x.UserId == userId)
.Select(x => new FavouriteProject
{
ProjectId = x.ProjectId,
}).ToList();
var resultset = await _projectrepository.Entity()
.Where(x => (filters.FiscalYear == null ||
filters.FiscalYear.Contains(Convert.ToInt32(x.ContractNo.ToString().Substring(0, 2)))) &&
(filters.IsFavourite == false &&
result.Contains(x.Id).ToString()))
.Select(x => new Tests
{
Id = x.Id,
Name = x.Name,
AdvertisementStatusId = x.AdvertisementStatusId,
CreatedOn = x.CreatedOn,
ContractNo = x.ContractN0,
})
.OrderBy(x => x.Name)
.ToListAsync();
Models:-
public class FavouriteProject
{
public int ProjectId { get; set; }
public string UserId { get; set; }
public virtual Project Project { get; set; }
}
public class Tests
{
public int Id { get; set; }
public string Name { get; set; }
public int? ContractNo { get; set; }
public int? AdvertisementStatusId { get; set; }
public DateTime? CreatedOn { get; set; }
public virtual ICollection<FavouriteCompany> FavouriteCompany { get; set; }
}
**filters**
public class ProjectFilters
{
public List<int> FiscalYear { get; set; }
public bool IsFavourite { get; set; }
}
**Project Model**
public class Project
{
public int Id { get; set; }
public string ProcurmentNo { get; set; }
public int? ContractNo { get; set; }
public int? ProjectSizeId { get; set; }
public int? SelectionProcedureId { get; set; }
public int? ResponseRequestedId { get; set; }
public int? AdvertisementStatusId { get; set; }
public int? NoOfPages { get; set; }
public string PreQualificationRequirements { get; set; }
public string Description { get; set; }
public string Name { get; set; }
public string MultilineDescription { get; set; }
public string BridgDesignWorkType { get; set; }
public string DbeMbeRequirements { get; set; }
public string SpecialNote { get; set; }
public decimal? BudgetAmount { get; set; }
public decimal? EstimatedContractAmount { get; set; }
public decimal? MaxumumContractAmount { get; set; }
public bool? IsMultipleContracts { get; set; }
public string ProposedScopeLoc { get; set; }
public string BondingRequirements { get; set; }
public string TechQuestionsAddressedTo { get; set; }
public int Status { get; set; }
public int? DistrictId { get; set; }
public DateTime? CreatedOn { get; set; }
public string ResponseEmail { get; set; }
public string ViewRFP { get; set; }
public string AdditionalInformation { get; set; }
public string LowBidDesign { get; set; }
public string AdminNote { get; set; }
public string MeetingLocationNotes { get; set; }
public string FdotKey { get; set; }
//public DateTime? EventDate { get; set; }
public bool IsUpdatesLocked { get; set; }
public bool? BdiProjectIndicator { get; set; }
public string LiabilityInsurance { get; set; }
public string ProjectThreshold { get; set; }
public int NumberOfContracts { get; set; }
public int? RelatedProjectCount { get; set; }
public string AdvertisementSpecialNotes { get; set; }
public string FMNSpecialNotes { get; set; }
public virtual ProjectSize ProjectSize { get; set; }
public virtual SelectionProcedure SelectionProcedure { get; set; }
public virtual ResponseRequested ResponseRequested { get; set; }
public virtual AdvertisementStatus AdvertisementStatus { get; set; }
public virtual District District { get; set; }
public virtual ICollection<ProjectEvent> ProjectEvents { get; set; }
public virtual ICollection<FinancialManagementNumber> FinancialManagementNumbers { get; set; }
public virtual ICollection<ProjectWorkGroup> ProjectWorkGroups { get; set; }
public virtual ICollection<ScrapedLink> ScrapedLinks { get; set; }
public virtual ICollection<ProjectStandardNote> ProjectStandardNotes { get; set; }
public virtual ICollection<FtpFileProject> FtpFileProjects { get; set; }
public virtual ICollection<CompanyProject> CompanyProjects { get; set; }
public virtual ICollection<ContactProject> ContactProjects { get; set; }
public virtual ICollection<ProjectUnderUtilizedWorkGroup> ProjectUnderUtilizedWorkGroups { get; set; }
public virtual ICollection<WorkProgram> WorkPrograms { get; set; }
[NotMapped]
public virtual ICollection<ScrapedProjectModel> ScrapedProjects { get; set; }
// public virtual FavouriteProject FavouriteProject { get; set; }
Can anyone give me simplified query to call both in single request?
And I have added my models and filters you can go through it I have used only one filters and the model of project because in that class only I have mentioned
Try the following query:
var query = _projectrepository.Entity().AsQueryable();
if (filters.FiscalYear?.Count > 0)
{
query = query.Where(x => filters.FiscalYear.Contains(Convert.ToInt32(x.ContractNo.ToString().Substring(0, 2))));
}
if (filters.IsFavourite)
{
var favouriteProjects = _favouriteProjectRepository.Entity()
.Where(x => x.UserId == userId);
query =
from p in query
join f in favouriteProjects on p.Id equals f.ProjectId
select p;
}
var resultset = await query
.Select(x => new Tests
{
Id = x.Id,
Name = x.Name,
AdvertisementStatusId = x.AdvertisementStatusId,
CreatedOn = x.CreatedOn,
ContractNo = x.ContractN0,
})
.OrderBy(x => x.Name)
.ToListAsync();

Migration failed while trying to create a many to many relationship

I am trying to connect two tables with a code first migration. I thought EF would create many to many relationship table itself but I get error "build failed". While building whole project everything works fine. It's just the migration.
Following are my models -
Task:
[Key]
public int Id { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? EndedAt { get; set; }
[Column(TypeName = "text")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "text")]
public string Title { get; set; }
[Required]
[Column(TypeName = "text")]
public string Description { get; set; }
public virtual TaskGroups TaskGroup { get; set; }
public string Status { get; set; }
[Column(TypeName = "text")]
public string WantedUser { get; set; }
TaskGroup:
[Required]
public int Id { get; set; }
[Required]
public string GroupName { get; set; }
public virtual Tasks Tasks { get; set; }
At first I've tried with ICollection<> but I got the same error.
My project is .Net Core 3.
Any ideas?
Edit
Tasks
[Key]
public int Id { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? EndedAt { get; set; }
[Column(TypeName = "text")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "text")]
public string Title { get; set; }
[Required]
[Column(TypeName = "text")]
public string Description { get; set; }
public string Status { get; set; }
[Column(TypeName = "text")]
public string WantedUser { get; set; }
public IList<TaskGroupTask> TaskGroupTask { get; set; }
TaskGroups
[Key]
public int Id { get; set; }
[Required]
public string GroupName { get; set; }
public IList<TaskGroupTask> { get; set; }
TaskGroupTask
public int TaskId { get; set; }
public int TaskGroupId { get; set; }
public Tasks Tasks { get; set; }
public TaskGroups TaskGroups { get; set; }
DbContext
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<TaskGroupTask>(e =>
{
e.HasKey(p => new { p.TaskId, p.TaskGroupId });
e.HasOne(p => p.Tasks).WithMany(t =>
t.TaskGroupTask).HasForeignKey(p => p.TaskId);
e.HasOne(p => p.TaskGroups).WithMany(tg =>
tg.TaskGroupTask).HasForeignKey(p => p.TaskGroupId);
});
}
public DbSet<TaskGroupTask> TaskGroupTask { get; set; }
You will need to create a joining entity, like MyJoiningEntity or TaskGroupTask, whose sole purpose is to create a link between Task and TaskGroup. Following models should give you the idea -
public class Task
{
public int Id { get; set; }
public string Description { get; set; }
public IList<JoiningEntity> JoiningEntities { get; set; }
}
public class TaskGroup
{
public int Id { get; set; }
public string GroupName { get; set; }
public IList<JoiningEntity> JoiningEntities { get; set; }
}
// this is the Joining Entity that you need to create
public class JoiningEntity
{
public int TaskId { get; set; }
public int TaskGroupId { get; set; }
public Task Task { get; set; }
public TaskGroup TaskGroup { get; set; }
}
Then you can configure the relation in the OnModelCreating method of your DbContext class, like -
modelBuilder.Entity<JoiningEntity>(e =>
{
e.HasKey(p => new { p.TaskId, p.TaskGroupId });
e.HasOne(p => p.Task).WithMany(t => t.JoiningEntities).HasForeignKey(p => p.TaskId);
e.HasOne(p => p.TaskGroup).WithMany(tg => tg.JoiningEntities).HasForeignKey(p => p.TaskGroupId);
});
This will define a composite primary key on JoiningEntity table based on the TaskId and TaskGroupId properties. Since this table's sole purpose is to link two other tables, it doesn't actually need it's very own Id field for primary key.
Note: This approach is for EF versions less than 5.0. From EF 5.0 you can create a many-to-many relationship in a more transparent way.
Since I have some time, I've decided to pull all pices of the code in one place. I think it would be very usefull sample how to create code-first many-to-many relations for database tables. This code was tested in Visual Studio and a new database was created without any warnings:
public class Task
{
public Task()
{
TaskTaskGroups = new HashSet<TaskTaskGroup>();
}
[Key]
public int Id { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTime? EndedAt { get; set; }
[Column(TypeName = "text")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "text")]
public string Title { get; set; }
[Required]
[Column(TypeName = "text")]
public string Description { get; set; }
public string Status { get; set; }
[Column(TypeName = "text")]
public string WantedUser { get; set; }
[InverseProperty(nameof(TaskTaskGroup.Task))]
public virtual ICollection<TaskTaskGroup> TaskTaskGroups { get; set; }
}
public class TaskGroup
{
public TaskGroup()
{
TaskTaskGroups = new HashSet<TaskTaskGroup>();
}
[Required]
public int Id { get; set; }
[Required]
public string GroupName { get; set; }
[InverseProperty(nameof(TaskTaskGroup.TaskGroup))]
public virtual ICollection<TaskTaskGroup> TaskTaskGroups { get; set; }
}
public class TaskTaskGroup
{
[Key]
public int Id { get; set; }
public int TaskId { get; set; }
[ForeignKey(nameof(TaskId))]
[InverseProperty(nameof(TaskTaskGroup.Task.TaskTaskGroups))]
public virtual Task Task { get; set; }
public int TaskGroupId { get; set; }
[ForeignKey(nameof(TaskGroupId))]
[InverseProperty(nameof(TaskTaskGroup.Task.TaskTaskGroups))]
public virtual TaskGroup TaskGroup { get; set; }
}
public class TaskDbContext : DbContext
{
public TaskDbContext()
{
}
public TaskDbContext(DbContextOptions<TaskDbContext> options)
: base(options)
{
}
public DbSet<Task> Tasks { get; set; }
public DbSet<TaskGroup> TaskGroups { get; set; }
public DbSet<TaskTaskGroup> TaskTaskGroups { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Server=localhost;Database=Task;Trusted_Connection=True;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TaskTaskGroup>(entity =>
{
entity.HasOne(d => d.Task)
.WithMany(p => p.TaskTaskGroups)
.HasForeignKey(d => d.TaskId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_TaskTaskGroup_Task");
entity.HasOne(d => d.TaskGroup)
.WithMany(p => p.TaskTaskGroups)
.HasForeignKey(d => d.TaskGroupId)
.HasConstraintName("FK_TaskTaskGroup_TaskCroup");
});
}
}

Lazy, Eager and Explicit loading is not working in Entity Framework 5

Department class:
public partial class Department
{
public Department()
{
this.Courses = new HashSet<Course>();
}
public int DepartmentID { get; set; }
public string Name { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
Course class:
public partial class Course
{
public Course()
{
this.Enrollments = new HashSet<Enrollment>();
this.People = new HashSet<Person>();
}
public int CourseID { get; set; }
public string Title { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
}
Context:
lazy loading is enabled as below.
universityDBContext.Configuration.LazyLoadingEnabled = true;
universityDBContext.Configuration.ProxyCreationEnabled = true;
Lazy loading is not working in Entity Framework 5. Please help
public partial class Department
{
public Department()
{
this.Courses = new HashSet<Course>();
this.Students = new HashSet<Student>();
}
public int DepartmentID { get; set; }
public string Name { get; set; }
public decimal Budget { get; set; }
public System.DateTime StartDate { get; set; }
public Nullable<int> InstructorID { get; set; }
public byte[] RowVersion { get; set; }
public virtual ICollection<Course> Courses { get; set; }
public virtual ICollection<Student> Students { get; set; }
public virtual Person Person { get; set; }
}
public partial class Course
{
public Course()
{
this.Enrollments = new HashSet<Enrollment>();
this.People = new HashSet<Person>();
}
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
public byte[] rowversion { get; set; }
[ForeignKey("Department")]
public virtual Department Department { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
public virtual ICollection<Person> People { get; set; }
}

Getting ObjectContext error even after calling ToList

When calling the method directly below I get a ObjectDisposedException when calling Mapper.Map with the retrieved list.
System.ObjectDisposedException: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
public IEnumerable<Models.Provider> Get(string owner)
{
List<Data.Models.Provider> providers;
using (var db = new Data.ProviderDirectoryContext())
{
providers = db.Providers.Where(p => p.Owner.Name == owner).ToList();
}
var dtoProviders = Mapper.Map<List<Data.Models.Provider>, List<Models.Provider>>(providers);
return dtoProviders;
}
I previously had the code like this (below), I wasn't getting an error, but the database was getting pounded when doing the mapping, and it was taking too long. I don't want to hit the database, when doing the mapping.
public IEnumerable<Models.Provider> Get(string owner)
{
using (var db = new Data.ProviderDirectoryContext())
{
var providers = db.Providers.Where(p => p.Owner.Name == owner).ToList();
var dtoProviders = Mapper.Map<List<Data.Models.Provider>, List<Models.Provider>>(providers);
return dtoProviders;
}
}
How can I retrieve all the data before doing the mapping?
Here is the DbContext and the Data.Models.Provider for your reference.
public class ProviderDirectoryContext : DbContext
{
public DbSet<Owner> Owners { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<LocationAuditLog> LocationAuditLog { get; set; }
public DbSet<Office> Offices { get; set; }
public DbSet<OfficePhoneNumber> OfficePhoneNumbers { get; set; }
public DbSet<OfficeAuditLog> OfficeAuditLog { get; set; }
public DbSet<OfficeDay> OfficeDays { get; set; }
public DbSet<Provider> Providers { get; set; }
public DbSet<ProviderPhoneNumber> ProviderPhoneNumbers { get; set; }
public DbSet<ProviderAuditLog> ProviderAuditLog { get; set; }
public DbSet<ProviderType> ProviderTypes { get; set; }
public DbSet<ProviderSpecialty> ProviderSpecialties { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Provider>().HasRequired(cn => cn.Owner).WithMany().WillCascadeOnDelete(false);
modelBuilder.Entity<Office>().HasRequired(cn => cn.Owner).WithMany().WillCascadeOnDelete(false);
}
}
public class Provider
{
public int Id { get; set; }
public int OwnerId { get; set; }
public virtual Owner Owner { get; set; }
public int? ProviderTypeId { get; set; }
public virtual ProviderType ProviderType { get; set; }
public int? ProviderSpecialtyId { get; set; }
public virtual ProviderSpecialty ProviderSpecialty { get; set; }
[Required]
[StringLength(75)]
public string FirstName { get; set; }
[StringLength(75)]
public string MiddleName { get; set; }
[Required]
[StringLength(75)]
public string LastName { get; set; }
[StringLength(100)]
public string EmailAddress { get; set; }
public virtual ICollection<ProviderPhoneNumber> PhoneNumbers { get; set; }
public string Note { get; set; }
public DateTime? InactiveOn { get; set; }
public int OfficeId { get; set; }
public virtual Office Office { get; set; }
public virtual ICollection<ProviderAuditLog> AuditLog { get; set; }
[Required]
public DateTime CreatedOn { get; set; }
[Required]
[StringLength(75)]
public string CreatedBy { get; set; }
[Required]
public DateTime ModifiedOn { get; set; }
[Required]
[StringLength(75)]
public string ModifiedBy { get; set; }
}
Thanks for the help!
The problem is that the Models.Provider class contains other classes like Models.Office, and Models.PhoneNumbers that were not eagerly loaded by the query. In addition to that, the Models.Provider class needs to be flattened. The Mapper wants to recursively map everything, and it keeps going down to the next class. For example, Provider.Office.Location.Offices.
The solution is to flatten Models.Provider and add .Include() to the query so it eagerly loads the data required.
I'll clean this up a bit more, but this is currently working.
public IEnumerable<Models.Provider> Get(string owner)
{
List<Data.Models.Provider> providers;
using (var db = new Data.ProviderDirectoryContext())
{
providers = db.Providers
.Where(p => p.Owner.Name == owner)
.Include("ProviderType")
.Include("ProviderSpecialty")
.Include("Office")
.Include("PhoneNumbers")
.ToList();
}
var dtoProviders = Mapper.Map<List<Data.Models.Provider>, List<Models.Provider>>(providers);
return dtoProviders;
}
public class Provider
{
public int Id { get; set; }
public int OwnerId { get; set; }
public int OfficeId { get; set; }
public string OfficeName { get; set; }
public int? ProviderTypeId { get; set; }
public string ProviderTypeName { get; set; }
public int? ProviderSpecialtyId { get; set; }
public string ProviderSpecialtyName { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
public string Note { get; set; }
public DateTime? InactiveOn { get; set; }
public DateTime CreatedOn { get; set; }
public string CreatedBy { get; set; }
public DateTime ModifiedOn { get; set; }
public string ModifiedBy { get; set; }
}
I am not sure how much this will help with performance but declaring the variable you don't want to dispose outside the using statement should fix your dispose exception.
public IEnumerable<Models.Provider> Get(string owner)
{
IEnumerable<Models.Provider> dtoProviders;
using (var db = new Data.ProviderDirectoryContext())
{
List<Data.Models.Provider> providers = db.Providers.Where(p => p.Owner.Name == owner).ToList();
dtoProviders = Mapper.Map<List<Data.Models.Provider>, List<Models.Provider>>(providers);
}
return dtoProviders;
}

Entity Framework [Key] tag not being recognised

I am getting warning errors of no key having been defined for each of my class library classes despite the fact that I have the [Key] tag and including the System.ComponentModel.DataAnnotations namespace, here is my context:
Context:
namespace Project.Data
{
public class ProjectContext : DbContext, IProjectContext
{
public ProjectContext(string connString)
: base(connString)
{
this.Configuration.LazyLoadingEnabled = true;
Database.SetInitializer<ProjectContext>(new ProjectInitializer());
this.Database.CreateIfNotExists();
this.Database.Initialize(true);
}
public IDbSet<Article> Article { get; set; }
public IDbSet<Brand> Brand { get; set; }
public IDbSet<Colour> Colour { get; set; }
public IDbSet<Customer> Customer { get; set; }
public IDbSet<CustomerCredit> CustomerCredit { get; set; }
public IDbSet<Delivery> Delivery { get; set; }
public IDbSet<DesignerTicket> DesignerTicket { get; set; }
public IDbSet<EuroRate> EuroRate { get; set; }
public IDbSet<Gift> Gift { get; set; }
public IDbSet<GZero> GZero { get; set; }
public IDbSet<InvoiceStock> InvoiceStock { get; set; }
public IDbSet<PrintOptions> PrintOptions { get; set; }
public IDbSet<Product> Product { get; set; }
public IDbSet<ProductLocation> ProductLocation { get; set; }
public IDbSet<Sale> Sale { get; set; }
public IDbSet<SaleAccount> SaleAccount { get; set; }
public IDbSet<SalesToWeb> SalesToWeb { get; set; }
public IDbSet<Shop> Shop { get; set; }
public IDbSet<Staff> Staff { get; set; }
public IDbSet<Ticket> Ticket { get; set; }
public IDbSet<Transfer> Transfer { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
Context Interface:
namespace Project.Data
{
public interface IProjectContext
{
IDbSet<Article> Article { get; set; }
IDbSet<Brand> Brand { get; set; }
IDbSet<Colour> Colour { get; set; }
IDbSet<Customer> Customer { get; set; }
IDbSet<CustomerCredit> CustomerCredit { get; set; }
IDbSet<Delivery> Delivery { get; set; }
IDbSet<DesignerTicket> DesignerTicket { get; set; }
IDbSet<EuroRate> EuroRate { get; set; }
IDbSet<Gift> Gift { get; set; }
IDbSet<GZero> GZero { get; set; }
IDbSet<InvoiceStock> InvoiceStock { get; set; }
IDbSet<PrintOptions> PrintOptions { get; set; }
IDbSet<Product> Product { get; set; }
IDbSet<ProductLocation> ProductLocation { get; set; }
IDbSet<Sale> Sale { get; set; }
IDbSet<SaleAccount> SaleAccount { get; set; }
IDbSet<SalesToWeb> SalesToWeb { get; set; }
IDbSet<Shop> Shop { get; set; }
IDbSet<Staff> Staff { get; set; }
IDbSet<Ticket> Ticket { get; set; }
IDbSet<Transfer> Transfer { get; set; }
}
}
[Key] decorated class example:
namespace Project.Data
{
public class Article
{
[Key]
public int ArticleID;
public bool IsCore;
public string Make;
public string Product;
public decimal Sale;
public string Department;
public string Scale;
public string Detail;
public DateTime InDate;
public decimal Reduce;
public bool IsOnSale;
public string VAT;
public bool IsOnWeb;
public string ProductCode;
public string Pick;
public string MemoDetail;
public string LOC;
public string ColourCode;
public bool StatusFlag;
public string Terminal;
}
}
Despite have the [Key] placed on Article I am getting the following message for the article class as shown below and this is repeated for each of the classes:
Project.Data.Article: : EntityType 'Article' has no key defined. Define the key for this EntityType.
Anyone see what I am doing wrong here? would be greatly appreciated
Define the members of your class as public properties as opposed to public variables like you have here, by including {get; set;} at the end of the declaration