which is the best way or if you have seen an example of which is the best way or a way to display to the user a view with this model and if you have and link example will be better
public class Student
{
public int StudentID { get; set; }
public string FirstMidName { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public decimal? Grade { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
Related
I have two model classes "customerdetails" and "addresssdetails".my question is "one customer can save with multiple addresss?
namespace customer2.Models
{
public class customerdetails
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { set; get; }
[Key]
public int customerid { set; get; }
public string customername { set; get; }
}
public class addressdetails
{
public int addressno { set; get; }
public string street { set; get; }
public string landmark { set; get; }
public int pincode { set; get; }
}
public class MkContext : DbContext
{
public virtual DbSet<customerdetails> customers { get; set; }
public virtual DbSet<addressdetails> address { get; set; }
}
public class customerviewmodel
{
public customerdetails cd { set; get; }
public List<addressdetails> ad { set; get; }
}
}![enter image description here](https://i.stack.imgur.com/4QCA1.jpg)
Before I answer, just a quick tip:
Make sure to properly case your class names and properties to improve readability.
Here is a link with some guidelines
If you want to store a Customer with multiple AddressDetails, you will need to define a class and add it to the context.
public class Customer {
public int Id { get; set; }
public CustomerDetails CustomerDetails { get; set; }
public IList<AddressDetails> AddressDetails { get; set; }
}
Then add this to the context.
DB relationship:
1ApplicationUser : n Tickets
1ApplicationUser: n Activities
1Tickets:n Activites
I would like to design above database using code first. However,there's error warning
"Column name 'ApplicationUserId' in table 'Tickets' is specified more
than once."
However, I checked there's only one ApplicationUserId in Table Ticket as shown in below class.
Would anyone please help how to fix it? Thanks.
The entity class are as following:
Ticket Class
public class Ticket
{
public int ID { get; set; }
public Ticket()
{ TicketCreateDate = DateTime.Now; }
public DateTime TicketCreateDate { get; set; }
public int TicketCreateBy { get; set; }
public DateTime TicketCloseDate { get; set; }
public int TicketCloseBy { get; set; }
public DateTime LastTicketUpdateDate { get; set; }
public int LastUpdateBy { get; set; }//Ticket Last Update by
public DateTime TicketAssignedDate { get; set; }
public int TicketAssignedTo { get; set; }
public string TicketType { get; set; }
public string Priority { get; set; }// Ticket priority
public string TicketStatus { get; set; }
public string TicketDescription { get; set; }
public int DeviceUserID { get; set; }
public string DeviceBrand { get; set; }
public string DeviceType { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
Acitivity Class
public class Activity
{
public int ID { get; set; }
public Activity()
{
CreatedTime = DateTime.Now;
}
public DateTime CreatedTime { get; set; }
public string ActivityDetails { get; set; }
public int ApplicationUserID { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
public int TicketId { get; set; }
public virtual Ticket Ticket { get; set; }
}
ApplicationUser Class
public class ApplicationUser : IdentityUser
{
public virtual ICollection<Ticket> Tickets { get; set; }
public virtual ICollection<Activity> Activities { get; set; }
}
I'm trying to delete a record from database table but I get this error
The DELETE statement conflicted with the REFERENCE constraint "FK_dbo.TicketDetail_dbo.Person_PersonID". The conflict occurred in database "AbcDB", table "dbo.TicketDetail", column 'PersonID'
I'm sharing my code please guide me.
TicketDetail:
public class TicketDetail
{
[Key]
public int TicketDetailId { get; set; }
public int? GenericOrderId { get; set; }
public int? PartId { get; set; }
public int? JunkPartId { get; set; }
public int PersonID { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
public virtual Part Part { get; set; }
public virtual Ticket Ticket { get; set; }
public virtual Customer Customer { get; set; }
public virtual JunkPart JunkPart { get; set; }
public virtual ICollection<MailSystem> MailSystems { get; set; }
public virtual ICollection<MailLog> MailLogs { get; set; }
}
Person:
public abstract class Person
{
[Key]
public int PersonID { get; set; }
public string City { get; set; }
public DateTimeOffset DateAdded { get; set; }
}
Customer:
public class Customer : Person
{
public int? CountryId { get; set; }
public string Company { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public virtual ICollection<TicketDetail> TicketDetails { get; set; }
public virtual ICollection<MailSystem> MailSystems { get; set; }
public virtual ICollection<MailLog> MailLogs { get; set; }
public virtual Country Country { get; set; }
}
Migration code:
modelBuilder.Entity<Customer>()
.HasMany<TicketDetail>(c => c.TicketDetails)
.WithRequired(x => x.Customer)
.WillCascadeOnDelete(true);
The error happens when I try to delete any customerId.
I have searched internet but couldn't find any appropriate solution, please help me with this error.
I have 3 tables that needs to be combined in one table. On below you can see the codes.
The problem is each player could play on one or more team on each tournament. Players and teams arranger by every new tournament.
So how can i map it with fluent api or maybe there is a better way to solve it. Thanks from now on.
public class Player
{
public int PlayerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public DateTime TimeStamp { get; set; }
public ICollection<Team> Teams { get; set; }
public ICollection<Tournament> Tournaments { get; set; }
}
public class Team
{
public int TeamID { get; set; }
public string Name { get; set; }
public DateTime TimeStamp { get; set; }
public ICollection<Player> Players { get; set; }
public ICollection<Tournament> Tournaments { get; set; }
}
public class Tournament
{
public int TournamentID { get; set; }
public DateTime Date { get; set; }
public int PlaceID { get; set; }
public DateTime TimeStamp { get; set; }
public virtual Place Place { get; set; }
public ICollection<Player> Players { get; set; }
public ICollection<Team> Teams { get; set; }
}
public class BPContext : DbContext
{
public DbSet<Tournament> Tournaments { get; set; }
public DbSet<Place> Places { get; set; }
public DbSet<Table> Tables { get; set; }
public DbSet<Player> Players { get; set; }
public DbSet<Team> Teams { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
I don't see what are you trying to accomplish with this. You have tables that contain all players, all teams and all tournaments.
I suppose there will be a match to be played? What you can do is create another table Matches, to use entity similar to this:
public class Match
{
[Key]
public int MatchId {get;set;}
[ForeignKey("Tournament")]
public int TournamentId {get;set;}
[InverseProperty("Matches")]
public virtual List<Team> Teams {get;set;}
[InverseProperty("Matches")]
public virtual List<Player> Players {get;set;}
[InverseProperty("Matches")]
public virtual Tournament Tournament {get;set;}
}
This new entity holds all 3 previous entities. However you have to modify previous ones to include these changes:
public class Player
{
public int PlayerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Phone { get; set; }
public DateTime TimeStamp { get; set; }
[InverseProperty("Players")]
public virtual List<Match> Matches { get; set; }
[InverseProperty("Players")]
public virtual List<Team> Teams {get;set;}
}
Getting all tournaments for player can be done with LINQ : ctx.Players.Where(x => x.PlayerId == 15).Matches.Select(x => x.TournamentId).
If you want to see all the players in a tournament: ctx.Matches.Where(x => x.TournamentId ==15).Players.Select(x => x.Name).
public class Team
{
public int TeamID { get; set; }
public string Name { get; set; }
public DateTime TimeStamp { get; set; }
[InverseProperty("Teams")]
public List<Match> Matches {get;set;}
[InverseProperty("Teams")]
public List<Player> Players {get;set;}
}
public class Tournament
{
public int TournamentID { get; set; }
public DateTime Date { get; set; }
public int PlaceID { get; set; }
public DateTime TimeStamp { get; set; }
public virtual Place Place { get; set; }
[InverseProperty("Tournament")]
public virtual List<Match> Matches {get;set;}
}
Today I've been working with MVC for the first time. Also normally I use the EF with model first, but I wanted to try POCO.
So I've made my 3 entities and when I try to make a controller I get an error:
Unable to retrieve metadata for "BookExchange.Models.Exchange". Unable to determine the principal end of an association between the types "BookExchange.Models.Exchange" and "BookExchange.Models.Book". The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
My 3 classes:
public class Book
{
public int BookID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string ISBN10 { get; set; }
public int PersonID { get; set; }
public virtual Person Person { get; set; }
public virtual Exchange Exchange { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Exchange> Exchanges { get; set; }
}
public class Exchange
{
[Key]
public int BookID { get; set; }
public int PersonID { get; set; }
public DateTime ReturnDate { get; set; }
public virtual Person Person { get; set; }
public virtual Book Book { get; set; }
}
Does anyone know what I'm doing wrong? I don't want to lose the association properties.
Thanks in advance!
Try adding foreign key properties for your references. E.g.
public class Book
{
public int BookID { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string ISBN10 { get; set; }
public virtual Person Person { get; set; }
public int PersonID { get; set; }
public virtual Exchange Exchange { get; set; }
public int ExchangeID { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Exchange> Exchanges { get; set; }
}
public class Exchange
{
public int ExchangeID { get; set; }
public int BookID { get; set; }
public virtual Book Book { get; set; }
public int PersonID { get; set; }
public virtual Person Person { get; set; }
public DateTime ReturnDate { get; set; }
}
Also, take a look at ScottGu's post on code first and this EF post on conventions.
Try this: (Remove database, so EF will create new)
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public string ISBN { get; set; }
public virtual Person Person { get; set; }
public virtual Exchange Exchange { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
public virtual ICollection<Exchange> Exchanges { get; set; }
}
public class Exchange
{
public int Id { get; set; }
public DateTime ReturnDate { get; set; }
public virtual Person Person { get; set; }
public virtual Book Book { get; set; }
}
It's your one on one associations.
Remove one reference between exchange or book, so Code-first can decide which one is more important in your one on one relation (Book <--> Exchange)
If you want to know why, you should read this: