How to configuer 1 to 1/0 Relationship? - entity-framework-6

good day evryone.
I have an child entity can be created with/without parent entities ... where are the parent entities should be indecate to just only one child record... or not
and the parentclass in the child entity can be null.
so i need to configuer 1:1/0 relationship ... any one can help??
class ParentClass1
{
int Id {get; set;}
string StrValue {get; set;}
ChildClass ChildClass {get; set;}
}
class ParentClass2
{
int Id {get; set;}
string StrValue {get; set;}
class ChildClass ChildClass {get; set;}
}
ChildClass
{
int Id {get; set;}
string StrValue {get; set;}
int? ParentClass1Id {get; set;}
ParentClass1 ParentClass1 {get; set;}
int? ParentClass2Id {get; set;}
ParentClass2 ParentClass2 {get; set;}
}

Related

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;}
}

Multiple one-to-one relationships to same table

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; }
}

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;}
}

Relation Mapping Question

I am using EF 4.1 and POCO model (code only).
I have a Fiche entity which has Id as the primary key.
And FicheCancellation entity, which has a Id which holds the Id of the Fiche that is cancelled and a CancellationFicheId which holds the Id of the generated cancellation Fiche's Id
class Fiche {
public Guid Id {get; set;}
...
public virtual Fiche CancelledFiche {get; set;}
public virtual Fiche CancellationFiche {get; set;}
public virtual FicheCancellation Cancellation {get; set;}
}
class FicheCancellation {
// cancelled FicheId
public Guid CancelledFicheId {get; set;}
//generated cancellation ficheId
public Guid CancellationFicheId {get; set;}
public virtual Fiche CancelledFiche {get; set;}
public virtual Fiche CancellationFiche {get; set;}
....
some other fields about the operation
...
}
now how do I configure navigations above, either with ForeignKey attributes or with ModelBuilder;
1- so that I have access to the cancellation entity.
2- and if the Fiche entity is a cancellation fiche, have a reference to the cancelled fiche.

Resources