Multiple one-to-one relationships to same table - sqlite-net-extensions

Which is the best way to model multiple one-to-one relationships to a same table (sqlite-net-extensions)? I am looking for a solution like this:
Class WayBill
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
[ForeignKey(typeof(Organization ))]
public int ConsignerId {get; set;}
[OneToOne]
public Organization Consigner {get; set;}
[ForeignKey(typeof(Organization ))]
public int ConsigneeId {get; set;}
[OneToOne]
public Organization Consignee {get; set;}
}
Class Organization
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
}
Obviously, the above won't work.
I have considered other options too:
create a table (WayBillOrganization) that captures the role of organization:
Waybill -->> WayBillOrganization --> Organization
put the required inverse OneToMany properties in to the Organization.
Handle things manually (ie. store only primary keys in the WayBill class and load the Organizations separately).
Option 2. is something I want to avoid. Organizations are related to so many other classes (not in my example) and even in the case of a waybill, there's a few more relationships I didn't include into the example (carrier, cargo paying party, freight forwarder and so on). Besides, I'd rather use inverse properties only when I need to navigate (for example, I don't use organization to find waybills, so an inverse property is only an extra burden.)
Option 3. isn't that attractive either.
So the option 1. seems to be the way to go. But before going there, I'd like to know if the perfect-world solution in my example is indeed impossible.
So, my question is: Is there a way to model multiple one directional OneToOne relationships without explicitly declared inverse properties?

Your use case is supported in SQLite-Net Extensions. You only have to specify the foreign keys explicitly in the relationship attributes, because automatic discovery may not work as expected:
class WayBill
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public int ConsignerId {get; set;}
[OneToOne("ConsignerId")]
public Organization Consigner {get; set;}
public int ConsigneeId {get; set;}
[OneToOne("ConsigneeId")]
public Organization Consignee {get; set;}
}
class Organization
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
}
Inverse relationships to the same class are also supported, but must be also declared explicitly at both ends:
class WayBill
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public int ConsignerId {get; set;}
[OneToOne(foreignKey: "ConsignerId", inverseProperty: "ConsignerInverse")]
public Organization Consigner {get; set;}
public int ConsigneeId {get; set;}
[OneToOne(foreignKey: "ConsigneeId", inverseProperty: "ConsigneeInverse")]
public Organization Consignee {get; set;}
}
class Organization
{
[PrimaryKey, AutoIncrement]
public int Id {get; set;}
public string Name {get; set;}
[OneToOne(foreignKey: "ConsigneeId", inverseProperty: "Consignee")]
public WayBill ConsigneeInverse { get; set; }
[OneToOne(foreignKey: "ConsignerId", inverseProperty: "Consigner")]
public WayBill ConsignerInverse { get; set; }
}

Related

Create an entity call "Team" that has a Manager and collection of Marketing Officers using "User" entity in Entity Framework Core

I just want to confirm whether this is a good approach to model an entity as bellow and what are the basics rules that it brakes?
By doing so do I need to do anything OnModelCreating?
Usecase: There are two types of users. One is Manger, the other one is the Marketing Officer. Then the Manager wants to create his team.
So I have modeled it as follows.
User and Role entities
public class User {
public int Id {get; set;}
public string UserName {get; set;}
public virtual Role Designation {get; set;}
}
public class Role {
public int Id {get; set;}
public string RoleName {get; set;}
}
Then composed the Team entity.
public class Team {
public int Id {get; set;}
public int ManagerId {get; set;}
public virtual User Manager {get; set;}
public virtual ICollection<User> MarketingOfficers {get; set;}
}

OData Serialization

I'm running into a oData Serialization error with an object with multiple levels.
The structure of my class is as follows
class Cons{
public string Left {get; set;}
public string Right {get; set;}
public string Opr {get; set;}
}
class Rate {
public string Name {get; set;}
public ICollection<ICollection<Cons>> Rules {get; set;}
}
class Prd {
public string Name {get; set;}
public ICollection<ICollection<Cons>> Rules {get; set;}
public ICollection<ICollection<Rate>> Rates {get; set;}
}
I run into the following serialization whenever Cons collection of the Rate class has some data:
"message":"ODataResourceSerializer cannot write an object of type 'Collection(Edm.Cons)'
Any help is highly appreciated.

Entity Framework using foreign key as only primary key in one to many

I am using Entity Framework code first to design the database.
I have 2 models with One to Many relationship. One "Foo" can have many "FooData" as follows -
public class Foo {
[Key]
public string serialNumber{get; set;}
public int someNumber {get; set;}
public string someName {get; set;}
// Many more properties
// Navigation Collection
public virtual ICollection<FooData> FooDatas{get; set;}
}
public class FooData{
[Key]
[ForeignKey("foo")]
public string SerialNum {get; set;}
public DateTime SomeTime {get; set;}
public byte[] SomeData {get; set;}
// Navigation property
public virtual Foo foo {get; set;}
}
When I try to add a controller for "Foo" in MVC, using "Foo" as a scaffolding model, it gives me this error - "Multiplicity is not valid in Role. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1".
I would appreciate some help here.
Thank you
Because of how you have it defined the same key is used in both so it is a 1-to-1 relationship. If you want a real one to many you will need to add/create another field and set them up as a composite key on the FooData table/entity.
public class Foo {
[Key]
public string serialNumber{get; set;}
public virtual ICollection<FooData> FooDatas{get; set;}
}
public class FooData {
[Key, Column(Order = 0),ForeignKey("foo")]
public string SerialNum {get; set;}
[Key,Column(Order=1)]
public int DataId {get;set;}
public virtual Foo foo {get; set;}
}

validation filter for inherited property mvc model

I have a model imported from entity data framework
public class person
{
public string Name {get; set;}
}
and a viewmodel inherited from person
public class personviewmodel:person
{
public int Applied {get; set;}
}
I use personviewmodel as the model in my view. I would like to add error validation (data annotation) to 'Name' property without modifying the generated 'person' class. Can you please help get this?
You can implement validation as follows. But thing here you are not inheriting Person using person's object as is. Hope this will help you.
public class Person
{
[Required(ErrorMessage = "Message")]
public string Name {get; set;}
}
public class personviewmodel
{
public int Applied {get; set;}
public Person Person {get; set;}
}
or
public class Person
{
public string Name {get; set;}
}
public class Personviewmodel : Person
{
public int Applied {get; set;}
[Required(ErrorMessage="Name is required")]
public new string Name {get; set;}
}
or
public class Person
{
public virtual string Name {get; set;}
}
public class Personviewmodel : Person
{
public int Applied {get; set;}
[Required(ErrorMessage="Name is required")]
public override string Name {get; set;}
}

Entity framework code first - map class with List<> of another class to multiple tables?

I'm evaluating EF4 and have a pretty basic question...I think, that I can't seem to find an answer for..
take the following example:
public class Question
{
public string question {get;set;}
public string answer {get; set;}
}
public class Person
{
public string Id {get; set;}
public string Name {get; set;}
public List<Question> Questions {get; set;}
}
Then I have the following tables in the database
Person
(
id,
name
)
Question
(
id,
personId,
question,
answer,
)
Can I use the EF4 code first to map the Person class to the two tables, or do I ahve to restructure my POCO's first so the question class contains the id and personId - which is not something I would like to do.
Can I add something to the OnModelCreating to map the class as I need it to be mapped?
Thanks!
Ok here's what I've done for now - but it requires me having to restructure my question class...
public class Question
{
public int Id {get;set;} /* New */
public int PersonId {get;set;} /* New */
public string question {get;set;}
public string answer {get; set;}
public virtual Person PersonObj {get;set;}
}
public class Person
{
public string Id {get; set;}
public string Name {get; set;}
public List<Question> Questions {get; set;}
}
and added the following in the OnModelCreating event
modelBuilder.Entity<Person>().
HasMany(d => d.Questions).
WithRequired(c => c.Person).
HasForeignKey(c => c.PersonId).
WillCascadeOnDelete();
Not sure it's fully right...but seems to be working for now.

Resources