I'm trying to map two entities connected via linker table.
OrganizationVM = Organization -|---< AddressLinker >---|- Address
one to many many to one
My goal is to get Organization ViewModel with State property from Address entity.
string state = OrganizationVM.State
Does anyone know how do I do it?
Thanks!
public class OrganizationVM : BaseViewModel
{
[Display(Name = "Parent ID")]
[Required]
public int? ParentID { get; set; }
public string ParentInternalName { get; set; }
public string State { get; set; }
public string StateAbr { get; set; }
public string City { get; set; }
}
public class AddressLinker
{
public int ID {get; set;}
public int OrganizationID { get; set; }
public int AddressID { get; set; }
}
public class Address
{
public int ID {get; set;}
public string State { get; set; }
public string Code { get; set; }
public string City { get; set; }
}
public class Organization
{
public int ID {get; set;}
public int? ParentID { get; set; }
public string ParentInternalName { get; set; }
public bool StructuralNode { get; set; }
public string InternalName { get; set; }
public string ExternalShortName { get; set; }
public string ExternalFullName { get; set; }
public string State { get; set; }
public string Code { get; set; }
public string City { get; set; }
}
Related
I have some data from Facebook API and I need to store them on Azure SQL Db.
I created the models and I'm trying to set Foreign Keys to link the tables but I always have some errors.
My models:
public class FacebookDataUser
{
[Key]
[JsonProperty("id")]
public string FacebookDataUserId { get; set; }
public string name { get; set; }
public string birthday { get; set; }
public string email { get; set; }
public virtual Hometown hometown { get; set; }
public virtual Location location { get; set; }
public virtual Events events { get; set; }
public virtual Likes likes { get; set; }
public virtual Age_Range age_range { get; set; }
public string gender { get; set; }
}
public class Hometown
{
[Key]
[JsonProperty("id")]
public string HometownId { get; set; }
public string name { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Location
{
[Key]
[JsonProperty("id")]
public string LocationId { get; set; }
public string name { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Events
{
[Key]
public string EventsId { get; set; }
public Datum[] data { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Datum
{
[Key]
public string DatumId { get; set; }
public string description { get; set; }
public string name { get; set; }
public DateTime start_time { get; set; }
public string PlaceId { get; set; }
public Place Place { get; set; }
public int attending_count { get; set; }
public string type { get; set; }
public string rsvp_status { get; set; }
public DateTime end_time { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Place
{
[Key]
public string PlaceId { get; set; }
public string name { get; set; }
public string LocationEventId { get; set; }
public LocationEvent location { get; set; }
public string DatumId { get; set; }
public Datum Datum { get; set; }
}
public class LocationEvent
{
[Key]
public string LocationEventId { get; set; }
public string city { get; set; }
public string country { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public string state { get; set; }
public string street { get; set; }
public string zip { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Likes
{
// Doesn't have ID for Likes, but I need to have a Key in all classes.
// If I don't have, I get an exception
[Key]
public string LikesId { get; set; }
public Datum1[] data { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Datum1
{
[Key]
public string Datum1Id { get; set; }
public string category { get; set; }
public string name { get; set; }
public int fan_count { get; set; }
public string website { get; set; }
public string LocationId { get; set; }
public LocationEvent location { get; set; }
public string[] emails { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
public class Age_Range
{
// Doesn't have ID for Age_Range, but I need to have a Key in all classes.
// If I don't have, I get an exception
[Key]
public string Age_RangeId { get; set; }
public int min { get; set; }
public string FacebookDataUserId { get; set; }
public FacebookDataUser FacebookDataUser { get; set; }
}
I get this exception:
Unable to determine the principal end of an association between the types 'ApiGroma.Models.Age_Range' and 'ApiGroma.Models.FacebookDataUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
If I add [Required] on Age_Range, I get this exception from Facebook API:
"modelState": {
"facebookDataUser.age_range.FacebookDataUser": [
"The FacebookDataUser field is required."
So, I tried to fill the values of Foreign Keys "by hand" before the method Add in my [HttpPost] method.
facebookDataUser.hometown.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.location.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.age_range.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.likes.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
facebookDataUser.events.FacebookDataUserId = facebookDataUser.FacebookDataUserId;
db.FacebookDataUsers.Add(facebookDataUser);
But I keep receiving the exception.
What's the proper way to do this?
It's been 2 days since I began looking for a solution, reading Microsoft blogs and others, but I can't fix this.
OBS: I am creating the database inside the context class.
Database.SetInitializer<MobileServiceContext>(new CreateDatabaseIfNotExists<MobileServiceContext>());
As I mentioned in the comments you must have your users into the database before inserting data to other tables related to those users (foreign keys).
Insert into table with foreign key
#EDIT: as promised, here is some code. I recommend you updating your table to accept null values in public virtual Hometown hometown { get; set; } and others.
public class FacebookDataUser
{
public string FacebookDataUserId { get; set; } // You already have the primary key you need
public string name { get; set; }
public string birthday { get; set; }
public string email { get; set; }
public virtual Hometown hometown { get; set; }
public virtual Location location { get; set; }
public virtual Events events { get; set; }
public virtual Likes likes { get; set; }
public virtual Age_Range age_range { get; set; }
public string gender { get; set; }
public void InsertUser(FacebookDataUser Data, Likes MoreData)
{
using (SqlConnection myCon = new SqlConnection("connection_string"))
{
using (SqlCommand query = new SqlCommand("INSERT INTO users_table (#ID, ...) VALUES (ID, ...)", myCon))
{
query.Parameters.AddWithValue("#ID", Data.FacebookDataUserId);
// add more parameters...
try
{
myCon.Open();
query.ExecuteNonQuery();
}
catch(Exception e)
{
throw e;
}
finally
{
myCon.Close();
}
}
using (SqlCommand query = new SqlCommand("INSERT INTO likes_table (..., #USERID) VALUES (..., USERID)", myCon))
{
// add more parameters...
query.Parameters.AddWithValue("#USERID", Data.FacebookDataUserId); // you won't get any exception related to the foreign key because this user is already in the parent table
try
{
myCon.Open();
query.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
myCon.Close();
}
}
}
}
}
I didn't run a query to get the user ID as I mentioned because you already have it, just organizing the way you run your methods should be enough.
So, after some changes and compairing the codes, i got it working.
public class FacebookDataUser
{
[Key,JsonProperty("id")]
public string FacebookDataUserId { get; set; }
public string name { get; set; }
public string birthday { get; set; }
public string email { get; set; }
public virtual Hometown Hometown { get; set; }
public virtual Location Location { get; set; }
public virtual Events Events { get; set; }
public virtual Likes Likes { get; set; }
public virtual Age_Range Age_Range { get; set; }
public string gender { get; set; }
}
public class Hometown
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int HometownId { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Location
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LocationId { get; set; }
public string id { get; set; }
public string name { get; set; }
}
public class Events
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EventsId { get; set; }
[JsonProperty("data")]
public ICollection<EventData> EventDatas { get; set; }
}
public class EventData
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EventDataId { get; set; }
public string description { get; set; }
public string name { get; set; }
[Column(TypeName = "datetime2")]
public DateTime start_time { get; set; }
public virtual Place Place { get; set; }
public int attending_count { get; set; }
public string type { get; set; }
public string rsvp_status { get; set; }
[Column(TypeName = "datetime2")]
public DateTime end_time { get; set; }
}
public class Place
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PlaceId { get; set; }
public string id { get; set; }
public string name { get; set; }
[JsonProperty("location")]
public virtual LocationEvent LocationEvent { get; set; }
}
public class LocationEvent
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LocationEventId { get; set; }
public string city { get; set; }
public string country { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public string state { get; set; }
public string street { get; set; }
public string zip { get; set; }
}
public class Likes
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LikesId { get; set; }
[JsonProperty("data")]
public virtual ICollection<LikesData> LikesData { get; set; }
}
public class LikesData
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LikesDataId { get; set; }
public string id { get; set; }
public string category { get; set; }
public string name { get; set; }
public int fan_count { get; set; }
public string website { get; set; }
[JsonProperty("location")]
public virtual LocationEvent LocationEvent { get; set; }
[JsonProperty("emails")]
public virtual ICollection<string> emails { get; set; }
}
public class Age_Range
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Age_RangeId { get; set; }
public int min { get; set; }
}
currently I have this two models:
Contact.cs
public class Contact
{
public int ConctactId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
}
PhoneNumber.cs
public class PhoneNumber
{
public int PhoneNumberId { get; set; }
public string Number { get; set; }
public string Description { get; set; }
public PhoneNumberTypeEnum EnumType { get; set; }
}
My question is, what is a correct way to alter these two so I can have multiple instances of PhoneNumber linked to one Contact? Also, later I would like to display all contacts in View with corresponding phone numbers.
Change your models as following
public class Contact
{
public int ConctactId { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public string City { get; set; }
public virtual ICollection<PhoneNumber> PhoneNumbers { get; set; }
}
public class PhoneNumber
{
public int PhoneNumberId { get; set; }
public string Number { get; set; }
public string Description { get; set; }
public PhoneNumberTypeEnum EnumType { get; set; }
public int ContactId {get; set;}
public virtual Contact Contact{get; set;}
}
Hi guys I am spinning wheels on this one. I am using EF6 and ASP.Net 4.6. I have a given table which has student information and parent information. A student can have many parents and a parent can have many students. Call this table 'Contact'. I am to create a table called 'Request' which will hold information for a parent submitting a request for his student. I will create a lookup table with two columns, one for student id and one for parent id called 'StudentParents'. I want to be able to have the parent log in, select his student from a drop down of all of his students and submit the request. The many to many relationship is throwing me off as far as my include statements. How can I get EF to set up this structure so that when I GetRequest(id) I can get the Student info and the Parent info to be included? Here is my code that wont Include anything other than the request.
public class Contact
{
[Key]
public int id { get; set; }
public string student_id { get; set; }//This is the Student ID
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public string grade { get; set; }
public int current_school_id { get; set; }
public string current_school_name { get; set; }
[Display(Name = "Parent Last Name")]
public string contact_first_name { get; set; }
[Display(Name = "Parent Middle Name")]
public string contact_middle_name { get; set; }
[Display(Name = "Parent Last Name")]
public string contact_last_name { get; set; }
public string contact_relationship { get; set; }
[Display(Name = "Parent Email")]
public string contact_email { get; set; }
[Display(Name = "Parent Address")]
public string login { get; set; }//This is the Parent ID
public string Classif_description { get; set; }
}
public class Request
{
[Key]
public int id { get; set; }
public Student student_id { get; set; }
public Contact login { get; set; }
[Display(Name = "First School Choice")]
public string firstSchool { get; set; }
[Display(Name = "Second School Choice")]
public string secSchool { get; set; }
[Display(Name = "Rising Grade")]
public string rising_grade { get; set; }
public DateTime ReqSubmitted { get; set; }
public string ReqStatus { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string ModifBy { get; set; }
}
public class Parent
{
public int id { get; set; }
public string contact_first_name { get; set; }
public string contact_middle_name { get; set; }
public string contact_last_name { get; set; }
public string contact_relationship { get; set; }
public string contact_email { get; set; }
public string contact_address { get; set; }
public string contact_city { get; set; }
public string contact_zip { get; set; }
[Key]
public string login { get; set; }
public string contact_pw { get; set; }
public string phone { get; set; }
public string phone_type { get; set; }
public Parent() { }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
[Key]
public int id { get; set; }
public int student_id { get; set; }
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public DateTime birthdate { get; set; }
public string gender { get; set; }
public string grade { get; set; }
public string Fed_race_description { get; set; }
public string Classif_description { get; set; }
public int current_school_id { get; set; }
public string current_school_name { get; set; }
public int home_school_id { get; set; }
public string home_school_name { get; set; }
public Student()
{
this.Parents = new HashSet<Parent>();
}
public virtual ICollection<Parent> Parents { get; set; }
}
public class OEContext : DbContext
{
public OEContext() : base("name=OEContext")
{
}
public DbSet<Request> Requests { get; set; }
public DbSet<Parent> Parents { get; set; }
public DbSet<Contact> Contacts { get; set; }
public DbSet<Student> Students { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Parent>()
.HasMany(s => s.Students)
.WithMany()
.Map(x =>
{
x.MapLeftKey("login");
x.MapRightKey("student_id");
x.ToTable("StudentParents");
}
);
base.OnModelCreating(modelBuilder);
}
}
Changed the strategy. Made Request have many Contacts. So I added a constructor to the request:
public Request()
{
Contacts = new List<Contact>();
}
public virtual ICollection<Contact> Contacts { get; set; }
Next I changed the contact class:
public int ContactId { get; set; }
public Contact() { }
public virtual Request Request { get; set; }
With this relationship I can pull both Parents and a student from the Contacts associated with the Request.
my model classes first:
public class Person
{
[Required, Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string LearntSkillsAndLevelOfSkills { get; set; }
public string ProfileImage { get; set; }
public string City { get; set; }
public string PhoneNr { get; set; }
[Required]
public string Email { get; set; }
public string Hobbys { get; set; }
public string SkillsToLearn { get; set; }
public string Stand { get; set; }
public int YearsOfWorkExperience { get; set; }
public string HobbyProjectICTRelated { get; set; }
public string ExtraInfo { get; set; }
public string Summary { get; set; }
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual UserProfile profile { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public Nullable<int> ID { get; set; }
public virtual Person personprofile { get; set; }
}
when i run this however it gives me this exception: The principal end of this association must be explicitly configured
i've searched this error but it doesn't clarify it for me... so i absolutely have no clue how to fix this. Basically i want to link my Person class to the Userprofiles so that i can create a login mechanism that automatically lets 1 person who makes an account on the site get 1 Profile to Edit to his own information. He's however not allowed to modify other people their accounts.
I hope this makes my problem clear and that somebody can help me :). i'm using EF 6 btw and i get the error in the class InitializeSimpleMembershipAttribute that comes standard with the MVC example of ASP.net
Greetings and thanks in advance,
Marijn
I managed to get it working with these codes in the models:
[Table("UserProfile")]
public class UserProfile
{
[Key, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual Person personprofile { get; set; }
}
public class Person
{
[ForeignKey("profile"), Key]
public int UserId { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string LearntSkillsAndLevelOfSkills { get; set; }
public string ProfileImage { get; set; }
public string City { get; set; }
public string PhoneNr { get; set; }
[Required]
public string Email { get; set; }
public string Hobbys { get; set; }
public string SkillsToLearn { get; set; }
public string Stand { get; set; }
public int YearsOfWorkExperience { get; set; }
public string HobbyProjectICTRelated { get; set; }
public string ExtraInfo { get; set; }
public string Summary { get; set; }
public UserProfile profile { get; set; }
}
I want to create my first application using the EF 4.1 Code First Model. I want to model a magazine subscription but just want to check that my POCO classes are fit for purpose.
The following are my classes. Am I missing anything?
Should Customer be a member of Subscription or should it be just that List be a member of Customer?
Thanks.
public class Subscription
{
public int SubscriptionID { get; set; }
public string CardNumber { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal NetPrice { get; set; }
public decimal Tax { get; set; }
public decimal Discount { get; set; }
public string PromotionalCode { get; set; }
public Customer Customer{ get; set; }
}
public class Customer {
public int CustomerID { get; set; }
public string Title { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public PostalAddress PostalAddress { get; set; }
public string EmailAddress { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public DateTime DateOfBirth { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string SecurityQuestion { get; set; }
public string SecurityAnswer { get; set; }
public bool Valid { get; set; }
public IList<Subscription> Subscriptions { get; set; }
public bool MailMarketing { get; set; }
public bool PartnerMailMarketing { get; set; }
}
public class PostalAddress
{
public int ID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string Postcode { get; set; }
public string Region { get; set; }
public string Country { get; set; }
}
If a Customer have X suscriptions, every suscription should have the customerID so you need that in your Suscription class (public int CustomerID {get; set;}).
OTOH, I think that you have to put that Customer reference as virtual and the suscription one (I don't know why).
Maybe Im wrong but that works for me.
Anyway, what's your problem?
Your models look correct. You don't necessarily need the Customer property in the Subscription class, but it does help if you want to retrieve a specific Subscription and then want to find the customer that is tied to that subscription. In that instance you can then do var customer = mySub.Customer instead of querying for a customer with the specific subscription id.