EntityFramework Schema specified is not valid. Errors: - c#

Order Model
public partial class Orden
{
public Orden()
{
this.Orden_Bitacora = new HashSet<Orden_Bitacora>();
}
//Attributes list
public virtual ICollection<Orden_Bitacora> Orden_Bitacora { get; set; }
}
Orden_Bitacora Model
public partial class Orden_Bitacora
{
public int IdBitacora { get; set; }
public int IdOrden { get; set; }
public virtual Orden Orden { get; set; }
}
But when I try to create a Order always display me the message:
Schema specified is not valid. Errors:
The relationship 'OrdenexTModel.FK_Orden_Bitacora_Orden' was not
loaded because the type 'OrdenexTModel.Orden' is not available.
Its something wrong with the model declaration?
The relationship 'OrdenexTModel.FK_Orden_Bitacora_Orden' was not loaded because the type 'OrdenexTModel.Orden' is not available.

It cant find a Primary Key on Ordan and therefore the FK relationship will not work.
Add the PK to Orden
public partial class Orden
{
public int OrdenId { get; set; }
public Orden()
{
this.Orden_Bitacora = new HashSet<Orden_Bitacora>();
}
//Attributes list
public virtual ICollection<Orden_Bitacora> Orden_Bitacora { get; set; }
}
and you may need to add [Key] attribute to your Orden_Bitacora PK as it doesnt follow the Entity Framework naming convention
[Key]
public int IdBitacora { get; set; }
or
public int Orden_BitacoraId
Hope that helps

Go to EntityFramework .edmx file which will open an entity framework, Right click and select Update Model from Database, select okey it will get updated as changes might have done in database.

Related

EF Core 2.2.6: Unable to map 2 foreign keys to the same table

I am having issues trying to map two fields that are foreign keys into the same table. The use case is for a modifier and creator. My class already has the Ids, and then I wanted to add the full User object as virtual.
I am using a base class so that each of my tables have the same audit fields:
public class Entity
{
public long? ModifiedById { get; set; }
public long CreatedById { get; set; } = 1;
[ForeignKey("CreatedById")]
public virtual User CreatedByUser { get; set; }
[ForeignKey("ModifiedById")]
public virtual User ModifiedByUser { get; set; }
}
The child class is very simple:
public class CircleUserSubscription : Entity
{
[Required]
public long Id { get; set; }
public long SponsorUserId { get; set; }
[ForeignKey("SponsorUserId")]
public virtual User User { get; set; }
public long TestId { get; set; }
[ForeignKey("TestId")]
public virtual User Test { get; set; }
}
This is a standard junction table.
When I try to generate the migration, I am getting errors that I don't understand fully.
Unable to determine the relationship represented by navigation property 'CircleUserSubscription.User' of type 'User'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
I tried what this answer had, but the code is basically the same: https://entityframeworkcore.com/knowledge-base/54418186/ef-core-2-2---two-foreign-keys-to-same-table
An inverse property doesn't make sense since every table will have a reference to the user table.
For reference, here is the User entity:
public class User : Entity
{
public long Id { get; set; }
public string Username { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I am hoping you all can help me out, TIA :)
EDIT: One thing to note, all of this worked fine when the entity class was as follows:
public class Entity
{
public long? ModifiedById { get; set; }
public long CreatedById { get; set; } = 1;
}
It was only after I added the entity that things went awry.

Map One-To-Many to 2 tables at the same time

I have 2 tables IncomingCheck and OutgoingCheck which contain many History items. To achieve this I've used inheritance and inherited 2 classes for incoming and outgoing histories.
public class IncomingCheck
{
[Key]
public int Id { get; set; }
public virtual IList<IncomingCheckHistory> History { get; set; }
//... other stuff
}
public class OutgoingCheck
{
[Key]
public int Id { get; set; }
public virtual IList<OutgoingCheckHistory> History { get; set; }
//... other stuff
}
public class CheckHistory
{
[Key]
public int Id { get; set; }
//... other stuff
}
public class IncomingCheckHistory : CheckHistory
{
public IncomingCheck IncomingCheck { get; set; }
}
public class OutgoingCheckHistory : CheckHistory
{
public OutgoingCheck OutgoingCheck { get; set; }
}
This approach works perfectly. In my database I have a table called CheckHistories with 2 nullable columns named [IncomingCheck_Id] and [OutgoingCheck_Id].
Now I have created a view on IncomingCheck which I created in Sql Server Management Studio and I can get its data using EF:
public class ViewIncomingCheck
{
[Key]
public int Id { get; set; }
public virtual IList<IncomingCheckHistory> History { get; set; }
//... other stuff
}
So far so good, but now I want Entity Framework to automatically load the History items but when I try to load the data, I get the following error:
SqlException: Invalid column name 'ViewIncomingCheck_Id'.
Invalid column name 'ViewIncomingCheck_Id'.
My guess is that the EF is looking for a column named ViewIncomingCheck_Id in the IncomingCheckHistory table but can't find it.
Is there a way to tell EF that it should use IncomingCheck_Id instead of ViewIncomingCheck_Id?

ASP.NET MVC EF Migration Foreign Key error

I have two classes that have a 1 to many relationship which are Format class and Exam class. Thus, I use the Code First Migrations to add the navigation properties for each classes as well as a Foreign Key property. However, when I enter the command update-database, it presents me with this error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Exams_dbo.ExamFormats_ExamFormatID". The conflict occurred in database "aspnet-ExamBankSys-20161202012850", table "dbo.ExamFormats", column 'id'.
Format Model
public class ExamFormat
{
[Key]
public int id { get; set; }
public string FormatDesc { get; set; }
public virtual ICollection<Exam> Exams { get; set; }
}
Exam Model
public class Exam
{
[Key]
public int id { get; set; }
[DataType(DataType.Date)]
public DateTime ExamDate { get; set; }
public int ModuleId { get; set; }
public virtual Module Module { get; set; }
[ForeignKey("ExamFormat")]
public int ExamFormatID { get; set; }
public virtual ExamFormat ExamFormat { get; set; }
}
It works for the public int ModuleId with the navigation property of Module. However, the migration does not work for the class ExamFormat even when I specify the [ForeignKey] annotation. Any idea how to solve this?

Entity Framework Update nested list

I use Entity Framework 6 (Code First). I have a class:
public class DialogSession {...}
And another class with a list of DialogSession objects:
public class DialogUser
{
public int Id { get; set; }
public List<DialogSession> DialogSessions { get; set; }
}
I add DialogSession object to the list and then execute context.SaveChanges() as follows:
dialogUser.DialogSessions.Add(dialogSession);
context.SaveChanges();
But the foreign key of the dialogSession record still Null:
I've tried using many methods on the web like the follows but withoyt success:
context.DialogUsers.Attach(dialogUser);
context.Entry(dialogUser).State = EntityState.Modified;
context.SaveChangesExtention();
Does anyone know how to save inner objects (like the list) in the database using Entity Framework (6)?
From your question is not clear which relationship type do you have, so I guess you have One-to-Many and something like this should works:
public class DialogSession
{
public int DialogSessionId { get; set; }
public virtual DialogUser DialogUser { get; set; }
}
public class DialogUser
{
public int DialogUserId { get; set; }
public virtual ICollection<DialogSession> DialogSessions { get; set; }
}
Take a look at example how properly configure this type of relationship in this article.
If I am not wrong you should add
dialogUser.DialogSessions.Add(dialogSession);
context.Entry(dialogUser).State = EntityState.Modified;
context.SaveChanges();
This will mark the entity as modified and then the changes should be reflected on the db.
This could be done a more efficiently by marking singular properties as modified
dialogUser.DialogSessions.Add(dialogSession);
context.Entry(dialogUser).Property(u => u.dialogSession).IsModified = true;
context.SaveChanges();
Give it a try :)
Please see: https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx
You should use a virtual list for the child entity, and ensure that the DialogSessions class also refers back to its parent with a DialogUserId property (so named by convention)
The below works for me.
using System.ComponentModel.DataAnnotations;
public class DialogSession
{
[Key]
public int DialogSessionId { get; set; }
public int DialogUser { get; set; }
}
public class DialogUser
{
[Key]
public int DialogUserId { get; set; }
public virtual ICollection<DialogSession> DialogSessions { get; set; }
}

EF Mapping Table Error "Invalid column name XXX_Id"

I am having an issue mapping my tables together. I get the error:
Invalid column name 'Film_Id'.
Here are my Entities:
public class Film
{
[Key]
public Int32 Id { get; set; }
public String Title { get; set; }
public virtual ICollection<NormComparableFilm> NormComparableFilms { get; set; }
}
public class NormComparableFilm
{
[Key]
public Int32 Id { get; set; }
public Int32 FilmId { get; set; }
public Int32 ComparableFilmId { get; set; }
[ForeignKey("FilmId")]
public virtual Film Film { get; set; }
[ForeignKey("ComparableFilmId")]
public virtual Film ComparableFilm { get; set; }
}
Is there a custom mapping in the OnModelCreating() function that I need? I tried adding the following but it fails with a slightly different error:
modelBuilder.Entity<Film>()
.HasMany(f => f.NormComparableFilms)
.WithMany().Map(t => t.MapLeftKey("FilmId")
.MapRightKey("ComparableFilmId")
.ToTable("NormComparableFilms"));
The above gives this error:
Invalid object name 'dbo.NormComparableFilms1'.
I think I'm close but can't seem to get it just right. Any help would be appreciated.
The first error happened because you are creating two relationships between the same entities and Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.The reason that there are extra foreign keys (Film_ID) is that Code First was unable to determine which of the two properties in NormComparableFilm that return a Film link up to the ICollection<NormComparableFilm> properties in the Film class. To resolve this Code First needs a little of help . You can use InverseProperty data annotation to specify the correct ends of these relationships, for example:
public class NormComparableFilm
{
[Key]
public int Id { get; set; }
public int FilmId { get; set; }
public int ComparableFilmId { get; set; }
[ForeignKey("FilmId")]
[InverseProperty("NormComparableFilms")]
public virtual Film Film { get; set; }
[ForeignKey("ComparableFilmId")]
public virtual Film ComparableFilm { get; set; }
}
Or remove the data annotation you already are using and add just these configurations:
modelBuilder.Entity<NormComparableFilm>()
.HasRequired(ncf=>ncf.Film)
.WithMany(f=>f.NormComparableFilms)
.HasForeignKey(ncf=>ncf.FilmId);
modelBuilder.Entity<NormComparableFilm>()
.HasRequired(ncf=>ncf.ComparableFilm)
.WithMany()
.HasForeignKey(ncf=>ncf.ComparableFilmId);
If in the second relationship, the ComparableFilm navigation property is optional, you need to change the type of the corresponding FK as nullable:
public class NormComparableFilm
{
//...
public int? ComparableFilmId { get; set; }
}
And use this configuration:
modelBuilder.Entity<NormComparableFilm>()
.HasOptional(ncf=>ncf.ComparableFilm)
.WithMany()
.HasForeignKey(ncf=>ncf.ComparableFilmId);
About the second error, you are trying to call the Film table as NormComparableFilms that is the default name that EF will give by convention to the table represented by the NormComparableFilm entity.
if you need to rename one of your tables, you can use this configuration:
modelBuilder.Entity<Film>().ToTable("Films"));

Categories