Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any
of those dapper libraries?
Dapper.Contrib has built-in support for marking a column as computed: add ComputedAttribute to allow support for computed columns on Insert. Here's how it works:
class MyModel
{
public string Property1 { get; set; }
[Computed]
public int ComputedProperty { get; set; }
}
Properties marked with the Computed attribute will be ignored on inserts.
Dapper creator Sam Saffron has addressed this requirement in response to another SO user's questions here. Check it out.
Also, if you want to use the Dapper Extensions library that Sam has mentioned in his answer, you can get it from Github or via Nuget.
Here's an example of ignoring properties from the Library's Test Project.
using System;
using System.Collections.Generic;
using DapperExtensions.Mapper;
namespace DapperExtensions.Test.Data
{
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateCreated { get; set; }
public bool Active { get; set; }
public IEnumerable<Phone> Phones { get; private set; }
}
public class Phone
{
public int Id { get; set; }
public string Value { get; set; }
}
public class PersonMapper : ClassMapper<Person>
{
public PersonMapper()
{
Table("Person");
Map(m => m.Phones).Ignore();
AutoMap();
}
}
}
In my case I used Dapper.Contrib.
Using [Write(false)] attribute on any property should solve the problem.
Some also suggest using [Computed] attribute.
public class Person
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
[Write(false)]
public IEnumerable<Email> Emails { get; }
}
You can design a base class without the computed property and use that for your inserts.
class BasePerson
{
public String Name {get;set;}
}
class Person: BasePerson
{
public String ComputedProperty {get;set;}
}
Insert<BasePerson>(person);
For those not wanting to include DapperExtensions, DatabaseGenerated from the standard System.ComponentModel.DataAnnotations.Schema can be used also.
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
Related
Good day!
I'm having an issue with the DevExpress LookUpEdit I can't figure out what the problem is.
I'm use Entity Framework list as a datasource.
public partial class provider_scheme : BaseEntity
{
public provider_scheme()
{
}
public int Provider_Scheme_RowID { get; set; }
public int Currency_RowID { get; set; }
public string Provider_Scheme_Name { get; set; }
public virtual currency currency { get; set; }
}
public partial class currency : BaseEntity
{
public currency()
{
provider_scheme = new HashSet<provider_scheme>();
}
public int Currency_RowID { get; set; }
public string Currency_ISOCode { get; set; }
public virtual ICollection<provider_scheme> provider_scheme { get; set; }
}
I'm setting the Datasource property of the LookUpEdit to IEnumerable<provider_scheme>, and setting up two column field names in my LookUpEdit. One for 'Provider_Scheme_Name' and one for 'currency.Currency_ISOCode'. But for some reason only the 'Provider_Scheme_Name' column values are showing. I've also checked and the 'currency' navigation property is being loaded.
Thanks in advance for your help
A bit late for an answer, but you might consider using the GridLookupEdit control instead. It permits adding all the columns you want
I have 2 entities: Post and Comment
I have a Post instance and I want to get its first 10 comments.
Post:
public class Post
{
public int Id { get; set; }
[InverseProperty("Post")]
public virtual ICollection<Comment> Comments { get; set; }
}
Comment:
public class Comment
{
public int Id { get; set; }
[Required]
public string Text { get; set; }
public int PostId { get; set; }
[ForeignKey("PostId")]
public virtual Post Post { get; set; }
}
How many Comments will come from the database if I do this? All or just 10?
var comments = post.Comments.Take(10).ToList();
Is it more efficient to do this?
var comments = Db.Comments.Where(x => x.Post == post).Take(10).ToList();
If you use .Include() then the property with InverseProperty attribute will contain all Comments assigned to this Post.
So it's better not to use it if you want to get only the first 10 comments.
I want first to say sorry, because I'm new to this programming language, so forgive me if I say or do something wrong.
I created a project with 3 class libraries (1 of them contains the tables from sql server). I made a ViewModel based on the tutorial from: "http://tutlane.com/tutorial/aspnet-mvc/how-to-use-viewmodel-in-asp-net-mvc-with-example", and now I want to be able to update the data in those tables, but I don't know how. I tried to do something but it failed.
`namespace BOL2.ViewModel
{
public class NIRIO
{
[Key]
public int ID { get; set; }
public int Number { get; set; }
public Nullable Date { get; set; }
public int NirID { get; set; }
[DisplayName("TypeID")]
public int TipID { get; set; }
public int SupplierID { get; set; }
public int ProductID { get; set; }
public Nullable<System.DateTime> EntryDate { get; set; }
public Nullable<System.DateTime> ExitDate { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
public decimal Total { get; set; }
public BOL2.tbl_NIR tbnir;
public BOL2.tbl_I_O tblio { get; set; }`
This is my ViewModel. It contains data from those 2 tables (tbl_NIR, first 3, and the others from tbl_I_O. I saw something on my research that they had a repository class, but I don't now if I should do another class for the viewmodel or I sould use the 2 that I already have? Any help is greatly appreciated.
You could do something similar to this.
public void UpdateCar(CarViewModel viewModel)
{
using (DataContext context = new DataContext())
{
CarEntity dataModel = context.CarEntities.where(x => x.Id == viewModel.Id).First();
dataModel.Name = viewModel.Name;
dataModel.Type = viewModel.Type;
context.SaveChanges();
}
}
You need to create your model objects from your view model and set the values for the models.
I'm not quite sure what is the method you used and didn't work. Just in case you tried to update the tables through the view, then you cannot do that. Because you have join
inform IT explanation
I recommend you to create a stored procedure in your database. Then call the procedure trough the code. It's secure and fast. explained here
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Newbie here.
I've got a C# Windows Presentation Forms program and I'm trying to add a method that downloads the string of a JSON webpage and then creates a list of events (each of which has a list of teams attending) from the data in the page. The only problem is that the JSON.NET documentation is confusing me and I can't seem to get a grasp on JSON format in general.
For reference, there are two parts to this method:
1) get the list of "official" events (if you look you will notice there is an "official" boolean value in each event item) and create a string list containing each event "key" (the first value in each event item)
2) for each event "key", make a list of teams attending each event and sort each team by their "team_number"
Here is a test page for the event list. Here is the test page for the team list from one event.
My biggest problem with the JSON.NET library/documentation is that I can't seem to figure out how to take each item and isolate one specific value from it. For example, there are a whole bunch of values in each event item, but I only need the "official" value and the "key" value. Same with the teams, I only need the "team_number" value from each item.
Can you guys help me understand the library a little bit more or maybe even point me to the correct library if this is the wrong one for the job?
First thing to do is use json2csharp to generate the classes that represent your JSON.
public class Event
{
public string key { get; set; }
public string website { get; set; }
public bool official { get; set; }
public string end_date { get; set; }
public string name { get; set; }
public string short_name { get; set; }
public string facebook_eid { get; set; }
public string event_district_string { get; set; }
public string venue_address { get; set; }
public int? event_district { get; set; }
public string location { get; set; }
public string event_code { get; set; }
public int year { get; set; }
public List<object> webcast { get; set; }
public List<object> alliances { get; set; }
public string event_type_string { get; set; }
public string start_date { get; set; }
public int event_type { get; set; }
}
public class Team
{
public string website { get; set; }
public string name { get; set; }
public string locality { get; set; }
public int rookie_year { get; set; }
public string region { get; set; }
public int team_number { get; set; }
public string location { get; set; }
public string key { get; set; }
public string country_name { get; set; }
public string nickname { get; set; }
}
Now you can deserialize the JSON into lists of those objects and use LINQ to query what you need out of the list. I'm doing this from memory so it may need some tweaking to work.
var events = JsonConvert.Deserialize<IEnumerable<Event>>( eventJson );
var officialEvents = events.Where( e => e.official )
.Select( e => new { e.key, e.official } );
This is probably just because my knowledge with the EF Code First fluent API is lacking, but I'm stumped.
I want to model the following:
A Groups collection with Id and Name
A Users collection with Id and Name
Each user is assigned to exactly one primary group
Each user may have zero or many secondary groups
The table structure I'm going for would look like:
Groups
Id
Name
Users
Id
Name
PrimaryGroupId
SecondaryGroupAssignments
UserId
GroupId
I've been beating my head against a wall trying to model this with EF Code First, but I can't get it to accept both relationships between User and Group. Sorry for not posting any .NET code (I'm happy to), but it's probably all wrong anyway.
Is there a way to make EF model this? I'm assuming I have to do some sort of configuration with the Fluent API. Maybe a better question is: is there any good, definitive reference for the Fluent API?
Thanks!
Try this (untested):
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<User> PrimaryUsers { get; set; }
public virtual ICollection<User> SecondaryUsers { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int PrimaryGroupId { get; set; }
public virtual Group PrimaryGroup { get; set; }
public virtual ICollection<Group> SecondaryGroups { get; set; }
}
public class Context : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Group> Groups { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.HasRequired(u => u.PrimaryGroup)
.WithMany(g => g.PrimaryUsers)
.HasForeignKey(u => u.PrimaryGroupId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<User>()
.HasMany(u => u.SecondaryGroups)
.WithMany(g => g.SecondaryUsers)
.Map(m => m.MapLeftKey("UserId")
.MapRightKey("GroupId")
.ToTable("SecondaryGroupAssignments"));
}
}
Based on Ladislav's excellent answer, here's how to do it without using any mappings - just attributes applied to the Model classes themselves:
public class Group
{
public int Id { get; set; }
[MaxLength(300)]
public string Name { get; set; }
public ICollection<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
[MaxLength(300)]
public string Name { get; set; }
[ForeignKey("PrimaryGroup")]
public int PrimaryGroupId { get; set; }
[Required]
public Group PrimaryGroup { get; set; }
[InverseProperty("Users")]
public ICollection<Group> SecondaryGroups { get; set; }
}
Notes
If you want, you can add the virtual keyword to the 2 ICollections and the Group. This allows lazy-loading. Performance-wise, I don't recommend it, but it is possible.
I included MaxLength attributes with an arbitrary (but safe) length of 300, because putting strings out in EF without a MaxLength gets you low-performance NVarChar(MAX) columns. Totally irrelevant to what's being asked but better to post good code.
I recommend against class names "User" and "Group" for your EF classes. They're going to complicate any SQL you attempt to run later, having to type [User] and [Group] to access them, and complicate using these classes in MVC Controllers where your class User will conflict with the Context property User that gives you access to the Asp.Net Identity library.