Code first scaffolding .net core 3.1 not working correctly - entity-framework-core

I have 2 classes that I have defined in my .net core entity framework 3.1 application
Room and RoomTypes
public class Room
{
public int RoomId { get; set; }
[StringLength(250)]
public string Roomname { get; set; }
[StringLength(450)]
public string RoomDescription { get; set; }
[StringLength(50)]
public string Rate { get; set; }
[StringLength(50)]
public string RateSpecial { get; set; }
public bool? Available { get; set; }
[StringLength(350)]
public string PhotoPath { get; set; }
public bool? Internet { get; set; }
public bool? TVSet { get; set; }
public bool? ElectronicSafe { get; set; }
public bool? TeaCoffee { get; set; }
public bool? Linnin { get; set; }
public int? RoomTypeId { get; set; }
public virtual RoomType RoomType { get; set; }
}
and RoomTypes
public class RoomType
{
public int RoomTypeId { get; set; }
[Column("RoomType")]
[StringLength(90)]
public string RoomType1 { get; set; }
public virtual ICollection<Room> Rooms { get; set; }
}
But when I scaffold my models, the lookup field (RoomType) shows the ID field and not the type.
If I scaffold the exact same in MVC 5, it does it correctly and the lookup field shows type and not the ID.
Can anyone shed light on this issue ??
Thanks
Luke

I solved the issue: One of my columns name was the same as the table!!
public virtual RoomType RoomType { get; set; } and one of my fields was roomtype.
Hope this prevents someone from going through the misery, at first very hard to spot.
Happy days!
P.S. With MVC 5 scaffolding, it prevents error, it just renames the field by adding a 1. This does not happen in .net core.

Related

Entity Framework Db context issue in .net core related to Models

Am Trying to create Two Tables like bellow got some EF error.
public class Student : ModelsBase
{
public string AdharNumber { get; set; }
public byte Religion { get; set; }
public int CategoryID { get; set; }
public string Cast { get; set; }
public string SubCast { get; set; }
public string Photo { get; set; }
public DateTime DateOfJoining { get; set; } = DateTime.Now;
[Required]
public ICollection<Address> TemporaryAddress { get; set; }
[Required]
public ICollection<Address> PermanentAddress { get; set; }
}
public class Address : ModelsBase
{
public string DoorNo { get; set; }
public string StreetLocality { get; set; }
public string Landmark { get; set; }
public string City { get; set; }
public int Taluk { get; set; }
public int District { get; set; }
public int State { get; set; }
public string Pincode { get; set; }
public bool IsPermanent { get; set; } = true;
public bool IsDefault { get; set; } = true;
[ForeignKey("Student")]
public Guid StudentId { get; set; }
}
Getting the bellow error while trying to Run the "Add-Migration command"
Both relationships between 'Address' and 'Student.PermanentAddress' and between 'Address' and 'Student.TemporaryAddress' could use {'StudentId'} as the foreign key. To resolve this, configure the foreign key properties explicitly in 'OnModelCreating' on at least one of the relationships
Please help. Thanks!
Your issue is that from the Address side of things you have a Many-to-1 with a single Student, but from the Student side of things you want 2x 1-to-Many relationships.
Since The relationship is really just a 1-to-Many from the student that you want to discriminate between temporary and permanent addresses:
public class Student : ModelsBase
{
public string AdharNumber { get; set; }
public byte Religion { get; set; }
public int CategoryID { get; set; }
public string Cast { get; set; }
public string SubCast { get; set; }
public string Photo { get; set; }
public DateTime DateOfJoining { get; set; } = DateTime.Now;
[Required]
public ICollection<Address> Addresses { get; set; } = new List<Address>();
[NotMapped]
public ICollection<Address> TemporaryAddresses => Addresses.Where(x => !x.IsPermanent).ToList();
[NotMapped]
public ICollection<Address> PermanentAddresses => Addresses.Where(x => x.IsPermanent).ToList();
}
With 1-to-many collections I recommend initializing them to an empty list to avoid null reference exceptions especially if lazy loading is disabled.
The caveat here is that from EF's perspective, Student only has the Addresses collection, do not attempt to use either TemporaryAddresses or PermanentAddresses in a query expression as these are unmapped accessors. If you want to filter based on a permanent address you will have to do it through Addresses and include the condition on IsPermanent in the query.
For example:
// Not valid...
var studentsInDetroit = context.Students.Where(x => x.PermanentAddresses.Any(a => a.City == "Detroit")).ToList();
// Valid...
var studentsInDetroit = context.Students.Where(x => x.Addresses.Any(a => a.IsPermanent && a.City == "Detroit")).ToList();
Normally I don't recommend using unmapped accessors in entities because of this. It is generally better to leave entities representing pure domain/data state and project that down to view models which can be more concerned about splitting the data into a more palatable form for consumption.

Entity Framework attempting to select non-existing column

I'm using Entity Framework 6.0 and have defined 2 POCO's to map to my database:
[Table("PortfolioGroups")]
public class PortfolioGroup : AuditableEntity<int>
{
[Column("Company_Id")]
public int CompanyId { get; set; }
[ForeignKey("CompanyId")]
public Company Company { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public ICollection<PortfolioGroupItem> PortfolioGroupItems { get; set; }
public PortfolioGroup()
{
PortfolioGroupItems = new Collection<PortfolioGroupItem>();
}
}
And the PortfolioGroupItem:
[Table("PortfolioGroupItems")]
public class PortfolioGroupItem : AuditableEntity<int>
{
[Column("PortfolioGroup_Id")]
public int PortfolioGroupId { get; set; }
[ForeignKey("PortfolioGroupId")]
public PortfolioGroup PortfolioGroup { get; set; }
[Column("Trademark_Id")]
public int? TrademarkId { get; set; }
[ForeignKey("TrademarkId")]
public Trademark.Trademark Trademark { get; set; }
[Column("TrademarkRegistration_Id")]
public int? TrademarkRegistrationId { get; set; }
[ForeignKey("TrademarkRegistrationId")]
public TrademarkRegistration TrademarkRegistration { get; set; }
[Column("Domain_Id")]
public int? DomainId { get; set; }
[ForeignKey("DomainId")]
public Domains.Domain Domain { get; set; }
}
However - when I attempt to query the PortfolioGroups, Entity Framework for some reason attempts to query a field named "Trademark_Id" - which doesn't exist on the PortfolioGroup entity:
Context.PortfolioGroups.SingleOrDefault(i => i.Id == id && i.CompanyId == companyId);
Throws:
Invalid column name 'Trademark_Id'.
I've used this kind of setup other places in my application without any problems. I simply cannot find out why EF is trying to query a column that's not in my entity!
Any suggestions would be greatly appreciated. I'm at the end of my rope here.
Thanks guys! :)
The problem is that you've added a Navigation Property on Trademark that requires a Foreign Key on Portfolio Group:
public class Trademark
{
[Key]
public int Id { get; set; }
[MaxLength(250)]
[Required]
public string Name { get; set; }
[MaxLength(150)]
public string Filename { get; set; }
public ICollection<PortfolioGroup> PortfolioGroups { get; set; }
public Trademark()
{
PortfolioGroups = new Collection<PortfolioGroup>();
}
}
EF expects PortfolioGorup to have a Trademark_ID column to store which PortfolioGroups are associated with a Trademark.

Entity framework relation mapping is not working

print("Hello World !");
I'm a beginner into the .NET world, i'm trying to use entity framework but i have a problem, my relations entities are not loaded, i followed the microsoft documentation but.
Here are my models :
[Table("Path")]
public class Path
{
[Key]
public int PathId { get; set; }
[Required]
public string Title { get; set; }
public List<Image> Images { get; set; }
[Required]
public string Description { get; set; }
[Required]
public float LocationX { get; set; }
[Required]
public float LocationY { get; set; }
[Required]
public string QrCode { get; set; }
public List<Mark> Marks { get; set; }
}
[Table("Mark")]
public class Mark
{
[Key]
public int MarkId { get; set; }
public int Value { get; set; }
public int PathForeignKey { get; set; }
[ForeignKey("PathForeignKey")]
public Path Path { get; set; }
}
[Table("Image")]
public class Image
{
[Key]
public int ImageId { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public int PathForeignKey { get; set; }
[ForeignKey("PathForeignKey")]
public Path Path { get; set; }
[Required]
public string Source { get; set; }
}
For me, everything is ok, i even have the correct entries into my database :
database entries
But my relation dont load when i'm reading database into my controller.
[HttpGet]
public IEnumerable<Path> GetAll()
{
return _context.Paths.ToList();
}
results
I'm sorry, i guess it is probably a stupid beginner error but i dont see what i did wrong. I tried with various database system, with virtual or not ...
Mark your navigation properties as virtual. For example:
[Table("Mark")]
public class Mark
{
[Key]
public int MarkId { get; set; }
public int Value { get; set; }
public int PathForeignKey { get; set; }
[ForeignKey("PathForeignKey")]
public virtual Path Path { get; set; } //Added virtual keyword
}
and a second way to load navigation properties is to use Include while fetching data:
return _context.Marks.Include(x => x.Path).ToList();

Prevent webapi from returning all data from associated tables

I have a table called MemberCompany which has a record for each company a member has. the model is below. When i query it via a webapi method passing in the memberid, i can see in debug mode that it returns the one company for that member, however when i run it in the browser i can see it returns the entire list of members also. Is it possible to just return a collection of membercompany records without the two referenced tables? I commented out the initial code to include these two tables but they appear to still be being included in the response.
public partial class MemberCompany
{
public int id { get; set; }
public int membership_id { get; set; }
public string company_name { get; set; }
public string company_address1 { get; set; }
public string company_address2 { get; set; }
public string company_town_city { get; set; }
public Nullable<int> company_county { get; set; }
public string company_postcode { get; set; }
public string company_tel { get; set; }
public string company_fax { get; set; }
public string company_email { get; set; }
public string company_contact { get; set; }
public string company_web { get; set; }
public string company_country { get; set; }
public Nullable<System.DateTime> last_updated { get; set; }
public Nullable<decimal> latitude { get; set; }
public Nullable<decimal> longitude { get; set; }
public virtual counties counties { get; set; }
public virtual members members { get; set; }
}
WebAPI
[HttpGet("admin/api/membercompany/member/{member_id}")]
public IEnumerable<MemberCompany> GetByMember(int member_id)
{
var Companies = db.MemberCompanies
// .Include(t => t.counties)
//.Include(t => t.members)
.Where(m => m.membership_id == member_id);
return Companies.AsEnumerable();
}
Turn off lazy loading for the context. My best guess is it's on and the entities are loaded when the graph is serialized...
Note: that's actually a good idea in a web app and I'd recommend you do it globally, so that you don't get bitten by performance issues due to lazy loading later, and always know precisely what you'll return.

Data model for User Notifications of many Types

I am trying to figure out how to model notifications. This is what I tried.
public class NotificationType
{
public int NotificationTypeID { get; set; }
public string Type { get; set; }
public string Action { get; set; }
}
public class Notification
{
public int NotificationID { get; set; }
public bool ReadStatus { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
[ForeignKey("NotificationType")]
public int NotificationTypeID { get; set; }
public int CommentID { get; set; }
public int ProjectID { get; set; }
public Comment Comment { get; set; }
public Project Project { get; set; }
public NotificationType NotificationType { get; set; }
public User User { get; set; }
}
Frankly, I feel like I have no idea what I am doing still, but let me tell you what I was trying to do, if it's a bad idea, tell me, if not please tell me how I do it.
I have Notifications that are for actions that occur related to a Comment, Reply and Project - That's why I have all those navigation properties and fields. I want to basically use the NotificationType to determine what it is and then use the appropriate ID field to get the information to display the notification to a user.
First problem, I can't seem to make those ID (CommentID ProjectID ) fields Nullable so I don't always have to have them.
How to I make them nullable using data annotations and or the fluent api OR design my model better?
Make them nullable properties
public class Notification
{
public int NotificationID { get; set; }
public bool ReadStatus { get; set; }
public DateTime DateCreated { get; set; }
[ForeignKey("User")]
public int UserID { get; set; }
[ForeignKey("NotificationType")]
public int NotificationTypeID { get; set; }
public int? CommentID { get; set; }
public int? ProjectID { get; set; }
public Comment Comment { get; set; }
public Project Project { get; set; }
public NotificationType NotificationType { get; set; }
public User User { get; set; }
}
Then use the fluent API
modelBuilder.Entity<Notification>().HasOptional(n => n.Comment).WithMany()
.HasForeignKey(n => n.CommentID);
I found this a kind of complex but complete notification system : http://library.blackboard.com/ref/df5b20ed-ce8d-4428-a595-a0091b23dda3/Content/Mini_TOCs/mt_admin_app_system_manage_comm_notify_frame.htm
I think that many good ideas can be borrowed from it.