Navigation Property without Declaring Foreign Key
I believe, it is not possible to define the relationship only with data attributes. The problem is that EF's mapping conventions assume that Creator
and Modifier
are the two ends of one and the same relationship but cannot determine what the principal and what the dependent of this association is. As far as I can see in the list of supported attributes there is no option to define principal and dependent end with data annotations.
Apart from that, I guess that you actually want two relationships, both with an end which isn't exposed in the model. This means that your model is "unconventional" with respect to the mapping conventions. (I think a relationship between Creator
and Modifier
is actually nonsense - from a semantic viewpoint.)
So, in Fluent API, you want this:
modelBuilder.Entity<User>()
.HasRequired(u => u.Creator)
.WithMany();
modelBuilder.Entity<User>()
.HasRequired(u => u.Modifier)
.WithMany();
Because a User
can be the Creator or Modifier of many other User records. Right?
If you want to create these two relationships without Fluent API and only with DataAnnotations I think you have to introduce the Many-Ends of the associations into your model, like so:
public class User
{
public int UserId { get; set; }
[InverseProperty("Creator")]
public virtual ICollection<User> CreatedUsers { get; set; }
[InverseProperty("Modifier")]
public virtual ICollection<User> ModifiedUsers { get; set; }
[Required]
public virtual User Creator { get; set; }
[Required]
public virtual User Modifier { get; set; }
}
I assume here that Creator
and Modifier
are required, otherwise we can omit the [Required]
attribute.
I think it's a clear case where using the Fluent API makes a lot of sense and is better than modifying the model just to avoid Fluent configuration.
To add navigation property without foreign key in EF Core, DB-first migration with .NET Core Web API
I found out the answer for this.
In EF core 3.x the dbcontext created by DBFrist scaffolding is all partial classes.
So I did the following:
1. new partial class for context class - here i added the relationship of navigation property using OnModelCreatingPartial() method. Example below
public partial class dbContext : DbContext
{
partial void OnModelCreatingPartial(ModelBuilder builder)
{
builder.Entity<Packcomponent>()
.HasOne(p => p.Pack)
.WithMany(b => b.PackComponent)
.HasForeignKey(p => p.PackId);
}
}
extended the partial class in a new file and added navigation property there.
public partial class Packcomponent
{
public Pack Pack { get; set; }
}
public partial class Pack
{
public List PackComponent { get; set; }
}
This way upon scaffolding it did not overwrite custom navigation properties and I also could use this properties to do EF operations like .Include() and to save related entities as well. It is pretty awesome!!
Navigation property without foreign key, or fluent api version?
I tried the other way, created one such Employee
table and added as a model, it got me this:
public partial class Employee
{
public Employee()
{
Subordinates = new HashSet<Employee>();
}
public int Id { get; set; }
public string Name { get; set; }
public int? ManagerId { get; set; }
public virtual Employee Manager { get; set; }
public virtual ICollection<Employee> Subordinates { get; set; }
}
public class EmployeeConfiguration: EntityTypeConfiguration<Employee>
{
public EmployeeConfiguration()
{
ToTable("EmployeeDbContext", "dbo");
HasKey(p => p.Id).Property(p => p.Id)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(p => p.Name).HasMaxLength(50);
HasMany(p => p.Subordinates).WithOptional(p => p.Manager)
.HasForeignKey(p => p.ManagerId);
}
}
Create navigation property without foreign key
You can't drop the foreign key completely, otherwise, how do you expect the two entities (i.e, tables) to be linked? What you can do instead is have a nullable FK, which will effectively make the relationship zero-or-one to many.
In your GameLevel
class, add a navigation property as a collection of UserGameProfile
:
public class GameLevel
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public double PointsMin { get; set; }
public double PointsMax { get; set; }
public virtual ICollection<UserGameProfile> UserGameProfiles { get; set; }
}
Then in the UserGameProfile
class, make the property GameLevelId
nullable:
public class UserGameProfile
{
// ...
// ...
public int? GameLevelId { get; set; }
[ForeignKey("GameLevelId")]
public virtual GameLevel GameLevel { get; set; }
}
This should work without even having to use Fluent API.
EF Code First foreign key without navigation property, but with parent collection property
You can use WithRequired
method without parameter:
modelBuilder.Entity<Parent>()
.HasMany(p => p.Children)
.WithRequired()
.HasForeignKey(c => c.ParentId);
With
part can be left empty if there is no inverse navigation property.
EF Code First foreign key without navigation property
With EF Code First Fluent API it is impossible. You always need at least one navigation property to create a foreign key constraint in the database.
If you are using Code First Migrations you have the option to add a new code based migration on the package manager console (add-migration SomeNewSchemaName
). If you changed something with your model or mapping a new migration will be added. If you didn't change anything force a new migration by using add-migration -IgnoreChanges SomeNewSchemaName
. The migration will only contain empty Up
and Down
methods in this case.
Then you can modify the Up
method by adding the follwing to it:
public override void Up()
{
// other stuff...
AddForeignKey("ChildTableName", "ParentId", "ParentTableName", "Id",
cascadeDelete: true); // or false
CreateIndex("ChildTableName", "ParentId"); // if you want an index
}
Running this migration (update-database
on package manage console) will run a SQL statement similar to this (for SQL Server):
ALTER TABLE [ChildTableName] ADD CONSTRAINT [FK_SomeName]
FOREIGN KEY ([ParentId]) REFERENCES [ParentTableName] ([Id])
CREATE INDEX [IX_SomeName] ON [ChildTableName] ([ParentId])
Alternatively, without migrations, you could just run a pure SQL command using
context.Database.ExecuteSqlCommand(sql);
where context
is an instance of your derived context class and sql
is just the above SQL command as string.
Be aware that with all this EF has no clue that ParentId
is a foreign key that describes a relationship. EF will consider it only as an ordinary scalar property. Somehow all the above is only a more complicated and slower way compared to just opening a SQL management tool and to add the constraint by hand.
Related Topics
Dependency Injection with a Static Logger, Static Helper Class
ASP.NET Core MVC - Client-Side Validation for Custom Attribute
Internal Server Error While Running a Simple .Net Core MVC Application on Ubuntu 16.04
Are Get and Set Functions Popular with C++ Programmers
How to Stop an Application from Opening
Show Data in ASP.NET HTML Table
Mono on Debian: Could Not Find File "/Srv/Www/Proj/Bin\Roslyn\Csc.Exe"
Using Xmlarrayitem Attribute Without Xmlarray on Serializable C# Class
How to Generate Web Service Out of Wsdl
Shell Script File(.Sh) Does Not Run from C# Core on Linux
How to Open the Physical File Operating System Error 32
C# Dllimport with C++ Boolean Function Not Returning Correctly
Linux to Windows Bad Encoding Response
Extract Content from Div Tag C# Regex
Xamarin: Android: System.Unauthorizedaccessexception: Access to The Path Is Denied