Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

c# - ScriptIgnore attribute in Entity Framework 6 breaks foreign key attribute

We have an existing code-first database using .NET Framework 4.5.2 and Entity Framework 6.2.0 where models are defined in a separate classlib.

The DAL classlib defines its own models inheriting from the generic models.

In the DAL models the foreign keys are defined using the ForeignKey attribute since sometimes the default naming convention cannot be applied.

We added a ScriptIgnore attribute to the navigation properties in order to prevent serialization problems.

Everything works as expected, except that when adding a new migration it happens that "some" foreign keys are dropped, new columns are added with the default naming convention and foreign keys are applied to those new columns.

Just removing the ScriptIgnore attribute the migration is fine and the foreign keys are all kept as they are.

Not all foreign keys are dropped and recreated this way, for example on the same table there are 3 foreign keys and none of them follow the naming convention, therefore are decorated with the ForeignKey and ScriptIgnore attribute, but only one of them is dropped and recreated by the migration.

We cannot figure out in which case the ForeignKey attribute is not respected and what causes this behaviour.

public class Alarm
{
   // ...
   public int? idChannel { get; set; }
   public int? idDevice { get; set; }
   public int? idScenario { get; set;  }
   // ...
}

public class EFAlarm : Alarm
{
   [ForeignKey("idChannel"), ScriptIgnore]
   public virtual EFChannel Channel { get; set; } // This one is kept as is

   [ForeignKey("idDevice"), ScriptIgnore]
   public virtual EFDevice Device{ get; set; } // This one is kept as is

   [ForeignKey("idScenario"), ScriptIgnore]
   public virtual EFScenario Scenario { get; set; } // This one is dropped and recreated
}

Here an extract from the migration:

public override void Up()
{
   // ...
   DropForeignKey("dbo.fe_alarms", "idScenario", "dbo.scenario");
   DropIndex("dbo.fe_alarms", new[] { "idScenario" });
   AddColumn("dbo.fe_alarms", "Scenario_id", c => c.Int());
   CreateIndex("dbo.fe_alarms", "Scenario_id");
   AddForeignKey("dbo.fe_alarms", "Scenario_id", "dbo.scenario", "id");
   // ...
}

The ScriptIgnore Attribute is defined in the System.Web assembly. We tried using other custom attributed defined in different assembly but none of them seems to be causing this kind of problem. What could be the cause of this behaviour?

question from:https://stackoverflow.com/questions/65899437/scriptignore-attribute-in-entity-framework-6-breaks-foreign-key-attribute

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After doing a little research, it seems to be a known issue that has already been reported in April 2018. Adding [ScriptIgnore] to your model will sometimes generate problems in your migrations.

Unfortunately it doesn't seem to have been followed since then, and the issue is still open. You could probably follow that Github thread and comment that you have had the same problem.

I have two possible suggestions to do in the meantime:

  1. Split your domain entities objects from your data transfer objects (DTO). Then, annotate with [ScriptIgnore] the properties in the DTO and serialize the DTO instead of the entity (or just don't include the property in DTO). I would strongly suggest this approach, even if you hadn't come across this bug, because I always like to keep my domain objects as clean as possible (no DB nor serialisation attributes). The mapping of the Entites to the DTOs can be done fairly easily manually, but I would still recommend the use of the popular library Automapper.
  2. Keep everything as it is, but whenever you create a new migration, remember to manually edit the scaffolded code to remove the wrong drop/create of the FK properties that EF is trying to do.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...