I have person class :
public class Person :
{
public Guid Id { get; set; }
public Guid? PersonRealId { get; set; }
public Guid? PersonLegalId { get; set; }
public virtual PersonReal PersonReal { get; set; }
public virtual PersonLegal PersonLegal { get; set; }
}
the real one :
public class PersonReal
{
public Guid Id { get; set; }
public string Title { get; set; }
public Guid Sex { get; set; }
public string FirstName { get; set; }
}
and the legal one has :
public class PersonLegal
{
public Guid Id { get; set; }
public string Title { get; set; }
public string Name { get; set; }
public Guid? TopManager { get; set; }
public string NationalCode { get; set; }
}
the person is always one of the real or legal type and not both of the same time .
what is the best pattern to implement person using design pattern?
Define an enum for specify the kind of users
public enum PersonTypes{
Real=0,
Legal=1,
}
public class Person
{
public string Id { get; set; }
public string NationalCode { get; set; }
public string Title { get; set; }
public bool Sex { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public PersonTypes Type { get; set; }
public string ManagerId { get; set; }
[ForeignKey("ManagerId")]
public virtual Person Manager{ 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;}
}
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've been working on this for a while now and i still haven't come up with a good idea.
I have the fallowing 3 types Project, User and Result. A user and a project can have multiple results. My problem now is that a result can be of multiple types (12 right now and that can change). I have no idea how i'm supposed to structure this the only things that this results have in common is a type and an Id and i want a table for each of the different 12 types.
So what i have until now is this.
Project
public class Project :ITypedEntity<ProjectType>
{
public int Id { get; set; }
public string Name { get; set; }
public string Nationality { get; set; }
public virtual ProjectType Type { get; set; }
public string TitleEn { get; set; }
public string TitleRo { get; set; }
public string Acronym { get; set; }
public string ContractNo { get; set; }
public virtual ActivityField ActivityField {get;set;}
public string SummaryEn { get; set; }
public string SumarryRo { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal Value { get; set; }
public bool IsPartner { get; set; }
public ICollection<User> Team{get;set;}
public string Website {get;set;}
public ICollection<INamedEntity> Results;
}
Result
public class Result:ITypedEntity<ResultType>
{
public int Id { get; set; }
public string Name{get;set;}
public ResultType Type { get; set; }
}
User
public class User:Person
{
public int CNP { get; set; }
public virtual Faculty Faculty { get; set; }
public bool IsUSAMV {get;set;}
public virtual ICollection<INamedEntity> Results {get;set;}
public virtual ICollection<Project> Projects {get;set;}
}
I don't think this helps but i'll paste them anyway
public interface INamedEntity:IEntity
{
string Name{get;set;}
}
public interface ITypedEntity<TType>:INamedEntity{
TType Type { get; set; }
}
public interface IEntity
{
int Id { get; set; }
}
Update 1
Some results types
public class BookChapter:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Book Book {get;set;}
public int PagesInChapter {get;set;}
public string Pagination {get;set;}
}
public class Book:INamedEntity
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Person> Authors { get; set; }
public int Year { get; set; }
public Publisher Publisher {get;set;}
public int NumberOfPages { get; set; }
public string Pagination { get; set; }
public int ISBN { get; set; }
}
public class Patent:ITypedEntity<PatentType>
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Person> Authors { get; set; }
public virtual Person Holder{get;set;}
public string Number{get;set;}
public virtual PatentType Type { get; set; }
}
Sorry if the question is not clear, i'll update it with any other information you need.
Thanks.
I'm trying to create a POCO object called Friend.cs. I can't seem to create inline properties for the IList.
public class User
{
public string ID { get; set; }
public string Name { get; set; }
public string Rating { get; set; }
public string Photo { get; set; }
public string Reputation { get; set; }
public string Group { get; set; }
public string GroupColor { get; set; }
public string PostCount { get; set; }
public string PostPerDay { get; set; }
public string JoinDate { get; set; }
public string Views { get; set; }
public string LastActive { get; set; }
public string Title { get; set; }
public string Age { get; set; }
public string Birthday { get; set; }
public string Sex { get; set; }
public string LinkedIn { get; set; }
public string Facebook { get; set; }
public string Twitter { get; set; }
public IList<Friend> {get???
}
Thanks for the help!
You forget the name of the property:
public IList<Friend> Friends {get; set;}
should work.
Your missing the property name
ie
Public IList<Friend> Friends { get; set; }
public class User
{
public IList<Friend> Friends
{
get { return _friends; }
set { _friends = new List<Friend>(value); }
}
private List<Friend> _friends;
}