I have two Entities ItemChange and PK:
[Table("Sync_Changes")]
public class ItemChange
{
public Guid AgentId { get; set; }
public DateTime ChangeTime { get; set; }
[Required]
public string Descriminator { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid ID { get; set; }
[NotMapped]
public object Item { get; set; }
public virtual ICollection<PK> PKs { get; set; }
public virtual ChangeType State { get; set; }
}
And the PK class:
[Table("Sync_PKs")]
public class PK
{
public PK(string key, object value)
{
Key = key;
Value = value?.ToString();
ValueType = value?.GetType().Name;
}
[Key]
[Column(Order = 0)]
[ForeignKey(nameof(Change))]
public Guid ChangeId { get; set; }
[Key]
[Column(Order = 1)]
public string Key { get; set; }
[Required]
public string Value { get; set; }
[Required]
public string ValueType { get; set; }
public virtual ItemChange Change { get; set;
}
I want to group the ItemsChange based on the PKs, for example i have two ItemChange that have the Pks [({Key="Id",Value="1"},{Key="GroupId",Value="2"})]
and the ChangeType [Created,Updated].
i want to know how to do this with Linq, or Mysql Script.
Screenshots of the both tables:
Changes that i want to group theme base on the pks.
PKs
Related
I am using Entity Framework Code-First and I am new to that. Here are my models:
public class Tbl_Organization_Type
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public byte fld_organization_type_id { get; set; }
[MaxLength(100)]
public string fld_organization_type_name { get; set; }
public byte? fld_sort { get; set; }
public ICollection<Tbl_Organization> Tbl_Organization { get; set; }
}
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public long? fld_organization_parent_id_ref { get; set; }
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
When i add ( add-migration personnel_1 -context PersonnelDbContext )
it gives me below error :
The relationship from 'Tbl_Organization.fld_organization_type_id' to
'Tbl_Organization_Type.Tbl_Organization' with foreign key properties
{'fld_organization_type_id_ref' : Nullable} cannot target the
primary key {'fld_organization_type_id' : byte} because it is not
compatible.
Configure a principal key or a set of compatible foreign
key properties for this relationship.
That does not look like a correct model (fld_organization_type_id_ref is not shown or is mistyped). IAC, that field needs to be of the same type as the primary key for Tbl_Organization_Type (byte). Try:
public class Tbl_Organization
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long fld_organization_id { get; set; }
public byte? fld_organization_type_id_ref { get; set; } // This FK needs to match referenced PK type
// Are you missing or not showing fld_location_id_ref
[StringLength(500)]
public string fld_organization_name { get; set; }
[StringLength(200)]
public string fld_organization_address { get; set; }
[ForeignKey("fld_location_id_ref")]
public Tbl_Personnel_Location Tbl_Personnel_Location { get; set; }
[ForeignKey("fld_organization_type_id_ref")]
public Tbl_Organization_Type Tbl_Organization_Type { get; set; }
}
Similarly, you need to show/define the field fld_location_id_ref and match the type with the reference table Tbl_Personnel_Location.
I have Country, City, Region and "Account Address" tables.
I want to create foreign key columns in "Account Address" pointing to Country, City, Region tables.
I have this code but it throws an error on creating database
The property \u0027Account_Id\u0027 cannot be configured as a
navigation property. The property must be a valid entity type and the
property should have a non-abstract getter and setter. For collection
properties the type must implement
After New Edit
public class Cities
{
[Key]
public int City_Id { get; set; }
public string City_name { get; set; }
public int Country_Id { get; set; }
[ForeignKey("Country_Id")]
public Countries countries { get; set; }
}
public class Region
{
[Key]
public int Region_Id { get; set; }
public string Region_name { get; set; }
public int City_Id { get; set; }
[ForeignKey("City_Id")]
public Countries countries { get; set; }
}
public class Accounts
{
[Key]
public int Account_Id { get; set; }
public string Fullname { get; set; }
public string Email { get; set; }
public string password { get; set; }
public int Cell_phone { get; set; }
public string Role { get; set; }
public int? estate_office_Id { get; set; }
[ForeignKey("estate_office_Id")]
public Estate_office estate_office { get; set; }
public List<Ads> ads { get; set; }
}
public class Account_address
{
[Key]
public int Id { get; set; }
[ForeignKey("Account_Id"), Column(Order = 0)]
public int Account_Id { get; set; }
[ForeignKey("Country_Id"), Column(Order = 1)]
public int Country_Id { get; set; }
[ForeignKey("City_Id"), Column(Order = 2)]
public int City_Id { get; set; }
[ForeignKey("Region_Id"), Column(Order = 3)]
public int Region_Id { get; set; }
public Accounts accounts { get; set; }
public Countries countries { get; set; }
public Cities cities { get; set; }
public Region region { get; set; }
}
You need to define public properties as shown below on the Account_address class.Then only EF will know how to map those navigation properties correctly.
public class Account_address
{
......
......
public Accounts accounts { get; set; } //like this
public Countries countries { get; set; } //like this
public Cities cities { get; set; } //like this
public Region region { get; set; } //like this
}
Update :
Hence you're not using singular naming convention for the classes,you have encountered this issue.Either you have to change the name of classes as singular or need to change the navigational property names a shown below.You have to do this for all the places.Here I have shown only for the Accounts class related navigational property.
[ForeignKey("Accounts_Id"), Column(Order = 0)]
public int Accounts_Id { get; set; }
My Advice is to follow the basic naming conventions.Then you can avoid lot of above kind of weird errors.
I am trying to create my first app using ASP.NET MVC framework and Entity Framework 6.
I chose to use code first approach and I started by defining my Models.
I have a model called Client with an identity attribute called Id. I have multiple Models that has an attribute called ClientId. The ClientId attribute should have virtual link to the Clients Model.
Here is how my Client model looks like
[Table("clients")]
public class Client
{
[Key]
public int id { get; set; }
public string name { get; set; }
public string status { get; set; }
public DateTime created_at { get; set; }
public DateTime? modified_at { get; set; }
public Client()
{
status = "Active";
created_at = DateTime.UtcNow;
}
}
Then here is how I am creating a belong to relation using other models.
[Table("BaseClientsToUsers")]
public class ClientToUser : ModelDefault
{
[ForeignKey("User")]
public int UserID { get; set; }
[ForeignKey("Client")]
public int ClientId { get; set; }
[ForeignKey("Team")]
public int DefaultTeamId { get; set; }
public DateTime? JoinedAt { get; set; }
public bool IsActive { get; set; }
public virtual User User { get; set; }
public virtual Client Client { get; set; }
public virtual Team Team { get; set; }
public ClientToUser()
{
DateTime UtcNow = DateTime.UtcNow;
IsActive = true;
CreatedAt = UtcNow;
LastUpdatedAt = UtcNow;
}
[Table("BaseTeams")]
public class Team : ModelDefault
{
[MaxLength(250)]
public string Name { get; set; }
[ForeignKey("Client")]
public int ClientId { get; set; }
public bool IsActive { get; set; }
public virtual Client Client { get; set; }
public Team()
{
DateTime UtcNow = DateTime.UtcNow;
IsActive = true;
CreatedAt = UtcNow;
LastUpdatedAt = UtcNow;
}
}
But, when I try to update my databases I get the following error
Introducing FOREIGN KEY constraint
'FK_dbo.BaseTeams_dbo.BaseClients_ClientId' on table 'BaseTeams' may
cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or
ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could
not create constraint or index. See previous errors.
I am not really sure what could be causing the error but it seems it is because I am creating multiple Foreign keys to the same `Clients model.
How can I fix this error?
Hello #Mike A When I started MVC I got this error too, so you need aditional tables that connects your DB items.
So try connect your database items with tables like that:
Here is my working example:
[Table("Products")]
public class Product
{
[Key]
public string Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal InternalPrice { get; set; }
public string Url { get; set; }
}
[Table("Categories")]
public class Category
{
[Key]
public string Id { get; set; }
[Required]
public string Name { get; set; }
public string Url { get; set; }
}
[Table("ProductCategories")]
public class ProductCategory
{
[Key]
[Column(Order = 0)]
public string ProductId { get; set; }
[Key]
[Column(Order = 1)]
public string CategoryId { get; set; }
public virtual Category Category { get; set; }
}
So you can connect your items without problems hope this will help you.
ConsumerNM is a NM/bridge table/entity.
Its a 1:N relation to Events table.
When I do an insert I get the question title exception.
What do I have to change to make it work?
DO I have to create 2 foreign keys each one pointing to the other ConsumerNM_Key?
public class ConsumerNM
{
public ConsumerNM()
{
Events = new HashSet<Event>();
}
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADMETA { get; set; }
[Key]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADCONSUMER { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
public class Event
{
[Key]
public int Id { get; set; }
public DateTime EventDate { get; set; }
public virtual ConsumerNM Consumer { get; set; }
[ForeignKey("Consumer")]
public int FK_Consumer { get; set; }
}
ConsumerNM or Event are incorrect. You have two options:
First Option:
public class ConsumerNM
{
//This is not a PK
//[Key]
//[Column(Order = 0)]
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int FK_LEADMETA { get; set; }
[Key]
public int FK_LEADCONSUMER { get; set; }
}
Event remains unchanged.
Second Option:
public class Event
{
[Key]
public int Id { get; set; }
public DateTime EventDate { get; set; }
public virtual ConsumerNM Consumer { get; set; }
[ForeignKey("Consumer")]
public int FK_LEADCONSUMER { get; set; }
[ForeignKey("Consumer")]
public int FK_LEADMETA { get; set; }
}
ConsumerNM remains unchanged.
In ASP.NET MVC 5, I have the following models:
JoinModel.cs
// Many-to-many join model
public class JoinModel
{
[Key]
[Column(Order = 0)]
public int ModelAID { get; set; }
[Key]
[Column(Order = 1)]
public int ModelBID { get; set; }
public virtual ModelA ModelA { get; set; }
public virtual ModelB ModelB { get; set; }
public virtual ICollection<ValueModel> Values{ get; set; }
}
ValueModel:
public class ValueModel
{
public int ID { get; set; }
// public int JoinModelID { get; set; }
[DefaultValue(0)]
public int Value { get; set; }
public virtual JoinModel JoinModel { get; set; }
}
I want change it in such a way that I have an ID property in JoinModel, so I can use JoinModelID in ValueModel. I also want to keep ModelAID and ModelBID as composite surrogate/candidate keys (just for uniqueness) and ID as primary key. Is there a way to do so?
Note: making all three ID, ModelAID and ModelBID as composite ID is not what I want.