Relation Mapping Question - entity-framework-4

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.

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

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

Breeze - properties of an expanded navigation property

Environment:
Breeze 1.4.16
Durandal 2.1.0
EF 6.1.1
Consider the following classes Account, Activity, and User. Let's call it a simple CRM scenario where a user creates sales activities for an account in their portfolio.
public class Account
{
public int Id {get; set;}
public ICollection<Activity> Activities {get; set;}
}
public class Activity
{
public int Id {get; set;}
public DateTime ActivityDate {get; set;}
public int AccountId {get; set;}
[ForeignKey("AccountId")]
public Account Account {get; set;}
public int CreatedById {get; set;}
[ForeignKey("CreatedById ")]
public User CreatedBy {get; set;}
}
public class User
{
public int Id {get; set;}
public string Name {get; set;}
}
I'm struggling to write a breeze query which when retrieving an Account, expands the related Activity entity to give me the Name of the user who entered the activity.
The following query works, however the createdBy property of each activity entity is always null:
var query = breeze.EntityQuery
.from("getAccount")
.withParameters({ id: id })
.expand("Activities");
Reading through the breeze documentation on Navigation Properties at: http://www.breezejs.com/documentation/navigation-properties, this seems close to the "OrderDetails.Product" scenario so I rewrote the query as follows:
This query throws an exception when I execute it.
var query = breeze.EntityQuery
.from("getAccount")
.withParameters({ id: id })
.expand("Activities.CreatedBy");

Resources