C # UWP Windows 10 project

Trying to figure out c EF7.

There are two classes (tables):

[Table("Transactions")] public class Transaction { [Key] public Guid Id { get; set; } publi List<Guid> Tags { get; set; } ... } [Table("Tags")] public class Transaction { [Key] public Guid Id { get; set; } ... } 

How to define such a connection in this framework?

UPD: The main thing is that the Tags field in the Transactions table is necessary in the form of a List<Guid> , because in this case, it's also JsonProperty , but you also need a connection for navigation ...

UPD2: Will this code work?

 [JsonProperty("tag")] List<Guid> TagsId { get; set; } [JsonIgnore, ForeignKey("TagsId")] public List<Tag> Tags { get; set; } 
  • You basically use EF7 , something I have not heard that it has already been officially released. EF6 not suitable? - sp7
  • @ sp7 is not important, but I did not find the EF6 documentation in WUP for Windows 10 - SYL

1 answer 1

As I understand it, do you want to have a Один ко Многим relationship between Transaction and Tag ?

Adjust your classes as follows:

 public class Transaction { public Guid Id { get; set; } public virtual List<Tag> Tags { get; set; } public Transaction() { Tags = new List<Tag>(); } } public class Tag { public Guid Id { get; set; } } 

Next, just create a context class:

 public class EFDbContext : DbContext { public DbSet<Transaction> Transaction { get; set; } public DbSet<Tag> Tags { get; set; } } 

And when you first access the context, everything else EF does for you

 class Program { static void Main(string[] args) { EFDbContext context = new EFDbContext(); context.Tags.Count(); Console.ReadKey(); } } 

Now, when accessing the transaction.Tags property from the database, all Tag entities associated with this transaction will be returned.


If you add the attribute [JsonProperty("tag")]

 Transaction tr = new Transaction() { Id = Guid.NewGuid() }; tr.Tags.Add(new Tag() { Id = Guid.NewGuid()}); tr.Tags.Add(new Tag() { Id = Guid.NewGuid() }); var json = JsonConvert.SerializeObject(tr); 

then JSON in your case will look like this:

{"Id":"a535ee64-48a8-4f53-89f2-a8afc5443ca2","tag":[{"Id":"a02fcf7f-75ff-4851-b8cf-cac050e0531d"},{"Id":"41aab533-a775-4050-a5e4-d386e2a4b3d7"}]}

  • public virtual List<Tag> Tags { get; set; } public virtual List<Tag> Tags { get; set; } - if this field is marked как [JsonProperty("tag")] , and List<Guid> should go to Json - will this work correctly? - SYL
  • @SYL, and why do you need JSON besides EF? I mean, why is information stored in two places at once? - Monk
  • @Monk In Json, it is not stored. She comes from him and then has to go to him to synchronize with the server - SYL
  • @Monk added a question - SYL
  • SYL, and why is the city in the code. This is at least incorrect from an architectural point of view. Why not create the necessary DTO and then serialize them into JSON ? - sp7