EF6 SQL Server Compact - sql

I'm trying to create a database using EF6 code-first and SQL Server Compact. After I execute update-database command I get the following result:
Screenshot SQL Server Compact
There are strange columns Operator_Id, Station_Id, Operator_Id1 and Station_Id1.
Here is my model:
public class OperatorActivity
{
public Guid Id { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool Synched { get; set; }
public virtual Station Station { get; set; }
public Guid StationId { get; set; }
public virtual Operator Operator { get; set; }
public Guid OperatorId { get; set; }
}
In SQL Server everything is fine
Screenshot SQL Server
Why do I get those columns in SQL Server Compact?

I think that the foreign key field of Operator is OperatorId (and similar for other) so you need to map it. If you map it with data annotation, for each foreign key field you need to map the field name, i.e.
[ForeignKey("OperatorId")]
public virtual Operator Operator { get; set; }
public Guid OperatorId { get; set; }

Related

How can I connect the AspNetUsers table from Identity with another table?

AspNetUsers table has an Id column that I want to use.
This is the "another table model" where I would like to create the fk for AspNetUsers table:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Project.Models
{
public class Profile
{
public int Id { get; set; }
public string Type { get; set; }
public string BusinessName { get; set; }
[DataType(DataType.ImageUrl)]
public string LogoUrl { get; set; }
public string Status { get; set; }
[ForeignKey("AspNetUsersId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
}
I include the Identity tables in my application database, not a separate database. As long as the Identity tables are in your database, you can create relationships.

'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or multiple cascade paths

I am trying in .NET EFCore the following Code-First migrations through the entities below
User
[Table("Users")]
public class User
{
[Key]
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string FirstName { get; set; }
[Required]
[MaxLength(100)]
public string LastName { get; set; }
[Required]
[MaxLength(250)]
public string Email { get; set; }
[Required]
[MinLength(8), MaxLength(16)]
public string Password { get; set; }
[Required]
[MinLength(6), MaxLength(15)]
public string Phone { get; set; }
public ICollection<Apartment> Apartments { get; set; }
public ICollection<Rating> Ratings { get; set; }
}
Apartment
[Table("Apartments")]
public class Apartment
{
[Key]
public int Id { get; set; }
[Required]
[MinLength(24), MaxLength(100)]
public string Title { get; set; }
[Required]
[MinLength(24), MaxLength(250)]
public string Address { get; set; }
[Required]
public int Price { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public User User {get; set;}
public ICollection<Rating> Ratings { get; set; }
public ICollection<AptCateg> AptsCategs { get; set; }
}
Ratings
[Table("Ratings")]
public class Rating
{
[Key]
public int Id { get; set; }
public int Value { get; set; }
[ForeignKey("Apartment")]
public int ApartmentId { get; set; }
public Apartment Apartment { get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
public User User { get; set; }
}
I use the commands dotnet ef migrations add InitialDatabase but when I try to use dotnet ef database update it throws the following error in cmd, as in the title
'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or
multiple cascade paths
I tried adding as in the EFCore tutorial from here the modelBuilder's Cascade behavior but it doesn't work because I am getting the same error. I have also tried doing the answer from here but the implementation for HasRequired isn't working even if try to install EntityFrameworkCore.Tools.
I understand that there is an issue with a circular thingy going on. From my intuition the program doesn't know what to do in the case of deleting a user, if to drop or not its ratings and apartments or some of that sort, and this is why its acting this way but I can't fix the problem.
My question is, how can I solve this issue as I cannot create my database, and thus I cannot continue working on the project.
Thanks!
You'll have to make the user relationship optional on one of the tables like:
public int? UserId { get; set; }
Making the property type nullable tells EF that a cascade delete is not required here.
You are causing a cyclic reference by adding the User and Apartment to the Ratings entity. User and Apartment entities already have a one-to-many relationship to the Ratings collection.
'FK_Ratings_Users_UserId' on table 'Ratings' may cause cycles or
multiple cascade paths
This is how the Ratings entity should look like:
[Table("Ratings")]
public class Rating
{
[Key]
public int Id { get; set; }
public int Value { get; set; }
}

Generate meta data from EntityFramework with partial class

I have a class like
using System;
using System.Collections.Generic;
public partial class Order
{
public Order()
{
this.OrderDetails = new HashSet<OrderDetail>();
}
public int OrderID { get; set; }
public string OrderNo { get; set; }
public System.DateTime OrderDate { get; set; }
public int BuyerID { get; set; }
public int UserID { get; set; }
public virtual Buyer Buyer { get; set; }
public virtual User User { get; set; }
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
}
which is provide by EntityFramework.
I have got meta data of the entityframework properly. But I want to add new two Columns as follows
public partial class Order
{
public string BuyerName { get; set; }
public string UserName { get; set; }
}
"After adding new columns when I fetch metadata but system does not return "BuyerName" and UserName" in metadata. what should Do?
I think "BuyerName" and UserName" are two new columns added to "Order" table.
In this case you need to update the Database Model (*.edmx file) (Update Model From DataBase) to get the mapping information for these new columns.
If you want to add only these properties to the Order class without any real columns in the database, then no need of update the Edmx file.

Fluent Nhibernate - One to Many Mapping - Child of same type as Parent

I have a class defined as:
public class ReportClient
{
public virtual int? Id { get; set; }
public virtual long? ClientId { get; set; }
public virtual string Name { get; set; }
public virtual string EmailAddress { get; set; }
public virtual string AdditionalEmailAddress { get; set; }
public virtual List<ReportClient> ChildClients { get; set; }
}
As you can see ChildClients are of same type as Parent.
Please guide me how can I map 'ChildClients' so for each ChildClient in List<ReportClient> ChildClients there is a new table record with a column 'ParentId' being set for this record ( having ParentId = Id)
Please guide.
Thank you!
I don't have the enviroment to test, but this should work, try swapping the column names if it doesn't.
HasManyToMany(x => x.ChildClients)
.ParentKeyColumn("ParentId")
.ChildKeyColumn("Id")

NHibernate - QBE

I have a problem using QBE with NHibernate. I have a one-to-one relationship between a Person class and an Employee.
public class Person
{
public virtual Employee Employee { get; set; }
public virtual int Age { get; set; }
public virtual string Forename { get; set; }
public virtual string Surname { get; set; }
public virtual int PersonID { get; set; }
}
public class Employee
{
public virtual int PersonID { get; set; }
public virtual string PayRollNo { get; set; }
public virtual int Holidays { get; set; }
public virtual Person Person { get; set; }
}
As an example, I want to get all Employees where Employee.Forename="John" and Employee.Person.PayRollNo = "231A". I was wondering if I could use Query By Example to do this?
I have not been able to find a definitive "no" but I haven't been able to get this work. I've found that QBE is promising but unfortunately not very useful due to the following limitations:
Cannot query related objects.
Requires public parameterless constructor.
Initialized properties are included in query unless specifically excluded using ExcludeProeprty. For example, bool properties are restricted to false in the where clause, DateTime as DateTime.MinValue. This makes the query very brittle because class modifications may have bad side effects.