Weird question here.
I have a bad situation with a database I can't change.
[Table("PROJTABLE")]
public class Certifikat {
[Key]
public long Recid { get; set; }
public String DATAAREAID { get; set; }
public String Projid { get; set; }
public virtual StandardAndScope StandardInfo { get; set; }
}
[Table("DS_CRT_PROJSTANDARDSCOPE")]
public class StandardAndScope {
[Key]
public long RECID { get; set; }
public String DATAAREAID { get; set; }
public String Standard { get; set; }
public String Scope { get; set; }
}
I have an optional one-to-many relationship from Certifikat to StandardAndScope. However! It's only one-to-many if the DATAAREAID column is a certain value ("crt").
Do I have any way of telling EntityFramework only to map rows where the value of that column is crt?
Related
Entity framework changed the column name in the DB, and isn't giving me it's value.
Here are my classes:
public class Settings
{
[Key]
public int ID { get; set; }
public string Setting { get; set; }
public string MoreDetail { get; set; }
public SettingTypes Type { get; set; }
public SettingGroups SettingGroup { get; set; }
public int? MinMembership { get; set; }
public string DefaultValue { get; set; }
public int? ParentID { get; set; }
public int Order { get; set; }
}
public class SettingTypes
{
[Key]
public int ID { get; set; }
[MaxLength(35)]
public string TypeName { get; set; }
}
public class SettingGroups
{
[Key]
public int ID { get; set; }
[MaxLength(35)]
public string GroupName { get; set; }
}
In the DB you can see that it changed the name of the two columns:
When I try to loop through the results, type is null:
How do I retrieve this value? I've tried renaming the columns in the class and in the DB but that just breaks more things. What's the proper way to handle this?
Thanks!
Dangit, I figured it out. Spent way to much time doing so.
It was as simple as adding "virtual" to the properties:
public virtual SettingTypes Type { get; set; }
public virtual SettingGroups SettingGroup { get; set; }
Now I can address it like:
setting.Type.TypeName
Hope this saves someone else some time.
I have the following class that I want to use as my data context in Entity Framework:
public class AggregateRecord : IAggregateRecord
{
[Key]
public int AggregateRecordID { get; set; }
public DateTime? InsertDate { get; set; } = DateTime.Now;
public DateTime BookingDate { get; set; }
public string AmountTypeName { get; set; }
public int? UnifiedInstrumentCode { get; set; }
public double? Amount { get; set; }
public string BookingAccountID { get; set; }
public string AccountCurrency { get; set; }
public string ClientCurrency { get; set; }
public string AffectsBalance { get; set; }
public string AssetType { get; set; }
public string UnderlyingInstrumentSubType { get; set; }
public string InstrumentSymbol { get; set; }
public string InstrumentSubType { get; set; }
public string UnderlyingInstrumentAssetType { get; set; }
public string UnderlyingInstrumentDescription { get; set; }
public string UnderlyingInstrumentSymbol { get; set; }
public string UnderlyingInstrumentUic { get; set; }
public double? AmountAccountCurrency { get; set; }
public string AmountClientCurrency { get; set; }
public string InstrumentDescription { get; set; }
public virtual ICollection<InstrumentInfo> InstrumentInfo { get; set; }
}
public class InstrumentInfo
{
[Key]
public int InstumentInfoID {get;set;}
public string SomeInformation { get; set; }
public int AggregateRecordID { get; set; }
public virtual AggregateRecord AggregateRecord { get; set; }
}
I have studies the examples provided for EF6 but I still have the problem that when I try to update my migration that I get the following error:
One or more validation errors were detected during model generation:
There are no primary or candidate keys in the referenced table 'dbo.AggregateRecords' that match the referencing column list in the foreign key 'FK_dbo.InstrumentInfoes_dbo.AggregateRecords_AggregateRecordID'.
Could not create constraint or index. See previous errors.
How do I have to define the classes so that InstrumentInfo can be accessed via a navigation property?
public class InstrumentInfo
{
[Key]
public int InstumentInfoID {get;set;}
public string SomeInformation { get; set; }
public int AggregateRecordId { get; set; }
public virtual AggregateRecord AggregateRecord { get; set; }
}
Seems you forgot "public"
I "solved" the problem. It's weird, but maybe it helps somebody in future that's why I answer my own question.
I renamed my class AggregateRecord to AggregateEntry. Performed the Add-Migration and Update-Database, with the new renamed class name. And it worked.
It looks like there was some problem with the migration definition or whatsoever, but it solved it.
In the end, I renamed it back to the original name, did the same procedure again and, voila, it works.
#Dennis Spade: Thanks for your effort, without your hint it would have taken me even more time to find the real "problem".
I have a bit of an issue and I need to know what the best design is for EF.
I have a Attribute model:
public enum TargetType
{
Attributes,
Availability,
Master
}
public class Attribute
{
public int Id { get; set; }
public int CriteriaId { get; set; }
[Required, MaxLength(100)] public string Name { get; set; }
[MaxLength(100)] public string Description { get; set; }
public TargetType Target { get; set; }
public virtual IList<Formula> Formulas { get; set; }
}
It has a list of Formulas:
public class Formula
{
public int Id { get; set; }
public int AttributeId { get; set; }
[Required]
[MaxLength(5)]
public string Operator { get; set; }
[Required]
[MaxLength(255)]
public string Expression { get; set; }
[MaxLength(100)]
public string Field { get; set; }
}
Which I match up in my DbContext like this:
modelBuilder.Entity<Attribute>().HasMany(m => m.Formulas).WithOptional().HasForeignKey(m => m.AttributeId);
The problem is, I now need to have Formulas as a separate property to another class Answer like this:
public class Answer
{
public int Id { get; set; }
public int QuestionId { get; set; }
[Required]
[MaxLength(255)]
public string Text { get; set; }
public int Order { get; set; }
public virtual IList<Formula> Formulas { get; set; }
}
The problem with this is Formula has AttributeId as a foreign key, so I can't really use the Formula model. I would have to create a new one like:
public class AnswerFormula
{
public int Id { get; set; }
public int AnswerId { get; set; }
[Required]
[MaxLength(5)]
public string Operator { get; set; }
[Required]
[MaxLength(255)]
public string Expression { get; set; }
[MaxLength(100)]
public string Field { get; set; }
}
But this seems counter-intuitive. So my question is, has anyone come across this before? If so, how did you solve the issue? For me, the problem is further enhanced when I start to use view models and factories.
Hi every one I want to save complex data using Entity Framework and C#. I have 2 classes Product and Order defined as follows
Product Class
public class Product
{
[Key]
public int Id { get; set; }
public string SKU_Code { get; set; }
public string Product_Name { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
public string Image { get; set; }
public DateTime Created_Date { get; set; }
public DateTime Modified_Date { get; set; }
}
Order Class
public class Order
{
[Key]
public long ID { get; set; }
public string Order_Id { get; set; }
public string Payment_Type { get; set; }
public string Customer_Name { get; set; }
public string Shipping_Address { get; set; }
public DateTime Order_Date { get; set; }
public DateTime Modified_Date { get; set; }
public bool Flag { get; set; }
public List<Product> ProductDetails { get; set; }
}
And I want to save data Order details and my piece of code is as follows.
public Order Add(Order odrerDetails)
{
using (var context = new EcommerceDBContext())
{
var MyOrder_Id = Helper.Random(7); //Generate random orderID from my class
foreach (var detail in odrerDetails.ProductDetails)
{
odrerDetails.Order_Id = MyOrder_Id;
odrerDetails.Quantity = Convert.ToInt32(detail.Quantity);
odrerDetails.Amount = Convert.ToDouble(detail.Price);
//Other Details
context.objOrderListing.Add(odrerDetails);
}
context.SaveChanges();
return odrerDetails;
}
}
This gives me perfect data but when it comes to context.SaveChanges(); it return's me error.
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.
To me you domain model seems all wrong. The order should just be used for grouping, its a typical e-commerce scenario.
When you get a receipt of your purchases, you get one receipt with every Item and price listed next to it. Its considered as one order of multiple things, not multiple orders of multiple things.
Reading your last comment, you cant have multiple orders with the same order id. Try to understand the domain first before trying to solve it with code. Also,you have no notion of a Customer with an Order.
public class Product
{
[Key]
public int Id { get; set; }
public string SKU_Code { get; set; }
public string Product_Name { get; set; }
public string Price { get; set; }
public string Image { get; set; }
public DateTime Created_Date { get; set; }
public DateTime Modified_Date { get; set; }
}
public class Order
{
[Key]
public long ID { get; set; }
public string Order_Id { get; set; }
public string Payment_Type { get; set; }
public string Customer_Name { get; set; }
public string Shipping_Address { get; set; }
public DateTime Order_Date { get; set; }
public DateTime Modified_Date { get; set; }
public bool Flag { get; set; }
public List<OrderLineItem> Items { get; set; }
}
public class OrderLineItem
{
[Key]
public long ID { get; set; }
public long Order_Id { get; set; }
public long Product_Id {get; set;}
public int Quantity {get; set;}
}
I have the following classes, a challenge has many submissions and it also has a winner.
The problem is that in my database Entity framework has created two columns on the Submissions table [ChallengeId] & [Challenge_ChallengeId] when I save an object it is added to the ChallengeId Column but relationship is being held on the Challenge_ChallengeId column.
Is there an issue with my models that is causing this or is some way I can set to hold the relationship and save to the same column?
Thanks!
public class Submission
{
[Key]
public Int32 SubmissionId { get; set; }
public DateTime Created { get; set; }
public Int32 ChallengeId { get; set; }
[ForeignKey("ChallengeId")]
public virtual Challenge Challenge { get; set; }
public String YouTubeVideoCode { get; set; }
public virtual IList<SubmissionVote> Votes { get; set; }
}
public class Challenge
{
[Key]
public Int32 ChallengeId { get; set; }
[Required]
public String ChallengeName { get; set; }
public virtual IList<Submission> Submissions { get; set; }
public Int32? OverallWinnerId { get; set; }
[ForeignKey("OverallWinnerId")]
public virtual Submission OverallWinner { get; set; }
}
I think you could benefit from just using the conventions and also using the shorthand keywords as data types. I think the error is because you have annotated the proxy with a ForeignKey attribute. Try this
public class Submission
{
public int SubmissionId { get; set; }
public DateTime Created { get; set; }
public int ChallengeId { get; set; }
public string YouTubeVideoCode { get; set; }
public virtual Challenge Challenge { get; set; }
public virtual IList<SubmissionVote> Votes { get; set; }
}
public class Challenge
{
public int ChallengeId { get; set; }
[Required]
public string ChallengeName { get; set; }
public int? OverallWinnerId { get; set; }
public virtual Submission OverallWinner { get; set; }
public virtual IList<Submission> Submissions { get; set; }
}
I'm pretty sure that Entity Framework uses a convention based approach, and the easiest way to fix this is to use the Id name instead of ChallengeId and SubmissionId
public class Submission
{
[Key]
public Int32 Id { get; set; }
public DateTime Created { get; set; }
public Int32 ChallengeId { get; set; }
[ForeignKey("ChallengeId")]
public virtual Challenge Challenge { get; set; }
public String YouTubeVideoCode { get; set; }
public virtual IList<SubmissionVote> Votes { get; set; }
}
public class Challenge
{
[Key]
public Int32 Id { get; set; }
[Required]
public String ChallengeName { get; set; }
public virtual IList<Submission> Submissions { get; set; }
public Int32? OverallWinnerId { get; set; }
[ForeignKey("OverallWinnerId")]
public virtual Submission OverallWinner { get; set; }
}