public class Customer
{
public Customer()
{
this.IsActive = 0;
this.IsDeleted = 0;
this.ActivationCode = "123456";
this.RegistrationDate = DateTime.Now;
this.CustomerRole = "1";
}
[Key]
public int Id { get; set; }
[Required]
[StringLength(30)]
public string Username { get; set; }
[Required]
[StringLength(30)]
public string Password { get; set; }
[DisplayName("E-mail")]
[StringLength(30)]
public string Email { get; set; }
public string ActivationCode { get; set; }
public int IsActive { get; set; }
public int IsDeleted { get; set; }
public DateTime RegistrationDate { get; set; }
public DateTime? LastLoginDate { get; set; }
public string CustomerRole { get; set; }
public static Customer Current { get; set; }
public List<Person> Person { get; set; }
public virtual ICollection<Person> Persons { get; set; }
}
public class AddressbookDBContext : DbContext
{
public DbSet<Customer> Customer { get; set; }
public DbSet<Person> Person { get; set; }
public DbSet<Address> Address { get; set; }
public DbSet<Group> Group { get; set; }
}
public class Address
{
[Key]
public int ID { get; set; }
[StringLength(50)]
public string Street { get; set; }
[DisplayName("House Number")]
[StringLength(20)]
public string HouseNumber { get; set; }
[DisplayName("Extenstion number")]
[StringLength(20)]
public string ExtensionNumber { get; set; }
[DisplayName("Postal")]
[StringLength(20)]
public string PostalCode { get; set; }
[StringLength(50)]
public string City { get; set; }
[StringLength(50)]
public string Country { get; set; }
public virtual ICollection<Person> Person { get; set; }
}
public class Person
{
[Key]
public int ID { get; set; }
[Required]
[DisplayName("Firstname")]
[StringLength(30)]
public string FirstName { get; set; }
[Required]
[DisplayName("Lastname")]
[StringLength(30)]
public string LastName { get; set; }
[DisplayName("Birthday")]
public DateTime BirthDay { get; set; }
public string Role { get; set; }
//one-to-many relationship
public int CustomerId { get; set; }
public virtual ICollection<Address> Address { get; set; }
public virtual ICollection<Group> Group { get; set; }
}
So the Customer is a Person itself and has an Address. Thereby a Customer can also have multiple Persons which can have more Addressess. If you see some mistakes above let me know. So now I have created a ViewModel.. which aint done yet.
public class CustomerViewModel
{
public Customer Customer { get; set; }
public Person Person { get; set; }
public Address Address { get; set; }
public static Customer CustomerMapper(CustomerViewModel customer)
{
return new Customer
{
Username = customer.Customer.Username,
Password = customer.Customer.Password,
Email = customer.Customer.Email
//Mapping aint complete....
};
}
}
Mapping aint correct... So what I want to do that if a customer goes to the customer edit page. Then the customer should see his data as a customer, as a person and address. So it is a big form with severall classess that apply to the same customer. Ofcourse there are many ways to simplify this, but I want something like this.
Now at the reposity class I need to define a method that will return a CustomerViewModel how do I do this within Entity framework.
var bleh = context.Customers.Find(id)
.Include(x => x.Persons);
The address class aint available. And ofcourse I want only the Person that matches the CustomerId. That is the property which defines if the Person belongs to that Customer. The Find method doesnt work, but without it I will get all customers. And I only
need the Customer that is logged in with his Person data and Address data. The address data is matched to the Person data.
This customer has also a first/last name and an address. So he should have a connection to the Person class and that class should have a many to many relationship with address. A person can life on more addressess. That is the customer... now the addressbook.
The Customer is having many contacts as in Persons... This is the same class as mentioned before. All those persons have also an address class.
When I have the database logic down then I still have the viewmodel problem :)
1. Customer: Customer class -> 1:1 PersonClass with CustomerId and N:N with Address class
2. Addressbook: Customer class -> contains 1:N Person Class and N:N with Address class
So Customer stores his own data to be able to send it to other customers. And then there is an addressbook.
As you can see I am using code first, but dont know for sure if my logic is all right. Please help me further with this. Is this a better explanation?
1. It's more clear now - however, you should be more attentive to those:
SpaceLama
A person can life on more addressess
SpaceLama
All those persons have also anaddress class.
I retain that
SpaceLama
Customer class -> 1:1 PersonClass with CustomerId and N:N with Address class
2. Now, the real question:
SpaceLama
As you can see I am using code first, but dont know for sure if my logic is all right
For code first, based on implicit mapping, it is very difficult for me to realize if it is correct or not. I suggest to you the FLUENT API for code first to define relationships - or make and edmx.
What would a fluent api be? What do you suggest that I should do? I mean... I have been thinking about this database design a lot, but apperantly I dont have the right set of knowledge yet to get a decent one. So I need some help and here I am. I could make
an address book within the most simple form.
You say that I need to be attentive with N:N relationships, why? If I made it correct the Code first should be the same as the Model first database. Ofcourse model first is more easily, but had never done it before.. so I thought to have it a try. I could
switch to model first and then get the classes out of it for my repository...right? I still wanna try code first.
entity frameworks support of n:n releastionships, either code first or database first, is pretty simple. it uses a join table, with 3 columns. unique index, table 1 key, table 2 key.
you need to read the framework docs. after defining the models, you build a context to support. you will need to use mapping to implement n:n.
Oke, do u have a simple example of that? Lets say with the Person and Address class..? Because you say context support? You mean the repository context? Because I already got the repository part working. And the mapping I would like an example of that. The
viewmodel u just pass the class Person and Address in it or do you make a whole new viewmodel class and convert this back and fort to the classess?
To make it more easy... I was thinking of the following. The customer can have his own Person class and Address class propertys... I will add those properties to the Customer class. So the customer has his own properties and the Person and Address class
are only used for the addressbook itself. It simplifies things isnt it? Or is that bad practise?
SpaceLama
Member
171 Points
136 Posts
repository with ViewModel
Jan 03, 2013 06:41 PM|LINK
Hello,
This will be my classess:
public class Customer { public Customer() { this.IsActive = 0; this.IsDeleted = 0; this.ActivationCode = "123456"; this.RegistrationDate = DateTime.Now; this.CustomerRole = "1"; } [Key] public int Id { get; set; } [Required] [StringLength(30)] public string Username { get; set; } [Required] [StringLength(30)] public string Password { get; set; } [DisplayName("E-mail")] [StringLength(30)] public string Email { get; set; } public string ActivationCode { get; set; } public int IsActive { get; set; } public int IsDeleted { get; set; } public DateTime RegistrationDate { get; set; } public DateTime? LastLoginDate { get; set; } public string CustomerRole { get; set; } public static Customer Current { get; set; } public List<Person> Person { get; set; } public virtual ICollection<Person> Persons { get; set; } } public class AddressbookDBContext : DbContext { public DbSet<Customer> Customer { get; set; } public DbSet<Person> Person { get; set; } public DbSet<Address> Address { get; set; } public DbSet<Group> Group { get; set; } } public class Address { [Key] public int ID { get; set; } [StringLength(50)] public string Street { get; set; } [DisplayName("House Number")] [StringLength(20)] public string HouseNumber { get; set; } [DisplayName("Extenstion number")] [StringLength(20)] public string ExtensionNumber { get; set; } [DisplayName("Postal")] [StringLength(20)] public string PostalCode { get; set; } [StringLength(50)] public string City { get; set; } [StringLength(50)] public string Country { get; set; } public virtual ICollection<Person> Person { get; set; } } public class Person { [Key] public int ID { get; set; } [Required] [DisplayName("Firstname")] [StringLength(30)] public string FirstName { get; set; } [Required] [DisplayName("Lastname")] [StringLength(30)] public string LastName { get; set; } [DisplayName("Birthday")] public DateTime BirthDay { get; set; } public string Role { get; set; } //one-to-many relationship public int CustomerId { get; set; } public virtual ICollection<Address> Address { get; set; } public virtual ICollection<Group> Group { get; set; } }So the Customer is a Person itself and has an Address. Thereby a Customer can also have multiple Persons which can have more Addressess. If you see some mistakes above let me know. So now I have created a ViewModel.. which aint done yet.
public class CustomerViewModel { public Customer Customer { get; set; } public Person Person { get; set; } public Address Address { get; set; } public static Customer CustomerMapper(CustomerViewModel customer) { return new Customer { Username = customer.Customer.Username, Password = customer.Customer.Password, Email = customer.Customer.Email //Mapping aint complete.... }; } }Mapping aint correct... So what I want to do that if a customer goes to the customer edit page. Then the customer should see his data as a customer, as a person and address. So it is a big form with severall classess that apply to the same customer. Ofcourse there are many ways to simplify this, but I want something like this.
Now at the reposity class I need to define a method that will return a CustomerViewModel how do I do this within Entity framework.
var bleh = context.Customers.Find(id) .Include(x => x.Persons);The address class aint available. And ofcourse I want only the Person that matches the CustomerId. That is the property which defines if the Person belongs to that Customer. The Find method doesnt work, but without it I will get all customers. And I only need the Customer that is logged in with his Person data and Address data. The address data is matched to the Person data.
Can someone help me with this?
Greetings,
Spacelama
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: repository with ViewModel
Jan 04, 2013 03:39 AM|LINK
1.
Your Customer class have not derived from Person class.
More, it is better to think to a "principal" Person from multiple that is that Customer.
2.
One single person or all his multiple persons?3.
Why?4.
Make a select.
5.
Generally,
just show ONE customer, not all .6.
Where is logged? Do you have an id, somewhat?
7. The whole thing is rather complicated - and your explanation does not help. Please seek someone that you can show your code - or explain better.
SpaceLama
Member
171 Points
136 Posts
Re: repository with ViewModel
Jan 04, 2013 08:59 AM|LINK
Oke, my explanation of my database:
A customer can log into my application.
Customer - Username, Password etc.
This customer has also a first/last name and an address. So he should have a connection to the Person class and that class should have a many to many relationship with address. A person can life on more addressess. That is the customer... now the addressbook. The Customer is having many contacts as in Persons... This is the same class as mentioned before. All those persons have also an address class.
When I have the database logic down then I still have the viewmodel problem :)
1. Customer: Customer class -> 1:1 PersonClass with CustomerId and N:N with Address class
2. Addressbook: Customer class -> contains 1:N Person Class and N:N with Address class
So Customer stores his own data to be able to send it to other customers. And then there is an addressbook.
As you can see I am using code first, but dont know for sure if my logic is all right. Please help me further with this. Is this a better explanation?
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: repository with ViewModel
Jan 04, 2013 09:15 AM|LINK
1. It's more clear now - however, you should be more attentive to those:
I retain that
2. Now, the real question:
For code first, based on implicit mapping, it is very difficult for me to realize if it is correct or not. I suggest to you the FLUENT API for code first to define relationships - or make and edmx.
SpaceLama
Member
171 Points
136 Posts
Re: repository with ViewModel
Jan 04, 2013 07:10 PM|LINK
What would a fluent api be? What do you suggest that I should do? I mean... I have been thinking about this database design a lot, but apperantly I dont have the right set of knowledge yet to get a decent one. So I need some help and here I am. I could make an address book within the most simple form.
You say that I need to be attentive with N:N relationships, why? If I made it correct the Code first should be the same as the Model first database. Ofcourse model first is more easily, but had never done it before.. so I thought to have it a try. I could switch to model first and then get the classes out of it for my repository...right? I still wanna try code first.
Any suggestions on next steps or database design?
Greetings,
Spacelama
bruce (sqlwo...
All-Star
37636 Points
5574 Posts
Re: repository with ViewModel
Jan 04, 2013 07:21 PM|LINK
entity frameworks support of n:n releastionships, either code first or database first, is pretty simple. it uses a join table, with 3 columns. unique index, table 1 key, table 2 key.
you need to read the framework docs. after defining the models, you build a context to support. you will need to use mapping to implement n:n.
SpaceLama
Member
171 Points
136 Posts
Re: repository with ViewModel
Jan 04, 2013 07:34 PM|LINK
Oke, do u have a simple example of that? Lets say with the Person and Address class..? Because you say context support? You mean the repository context? Because I already got the repository part working. And the mapping I would like an example of that. The viewmodel u just pass the class Person and Address in it or do you make a whole new viewmodel class and convert this back and fort to the classess?
To make it more easy... I was thinking of the following. The customer can have his own Person class and Address class propertys... I will add those properties to the Customer class. So the customer has his own properties and the Person and Address class are only used for the addressbook itself. It simplifies things isnt it? Or is that bad practise?
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: repository with ViewModel
Jan 05, 2013 10:27 AM|LINK
SpaceLama
Member
171 Points
136 Posts
Re: repository with ViewModel
Jan 05, 2013 04:14 PM|LINK
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: repository with ViewModel
Jan 05, 2013 06:46 PM|LINK
because code first is hard to program - and db first more easy - it has diagrams.