I need to create a list of classes. I don`t want to uses "Insert", since the amount of data is small and adding ll data all together might be more readable.
Unfortunatelly I even cant compile the code :
List<MyArg> perfTestsArgs = new List<MyArg>(
{ new MyArg("columns","1"),
new MyArg("rows","1"),
new MyArg("batch","1"),
new MyArg("trips","400"),
new MyArg("strict","true"),
new MyArg("valueLength","1"),
}); //<- problematic (why?)
public class MyArg
{
public string ArgName { get; set; }
public string ArgValue { get; set; }
public PerformanceTestsArg(string argName, string argValue)
{
ArgName = argName;
ArgValue = argValue;
}
}
I`ll appreciate the advice how to do that correctely
Remove the () from new List<MyArg>.
List<MyArg> perfTestsArgs = new List<MyArg>() // <- close the bracket here, or remove () as #Mihai said
{ new MyArg("columns","1"),
new MyArg("rows","1"),
new MyArg("batch","1"),
new MyArg("trips","400"),
new MyArg("strict","true"),
new MyArg("valueLength","1"),
};
more details on initializing objects here: http://msdn.microsoft.com/en-us/library/bb384062.aspx
Related
I am building an application on top of Entity Framework Core and I want to, sort of, apply a migration at runtime.
My intended approach is to have the current database model in memory and create a new model, then calculate the difference between the two models using IMigrationsModelDiffer.GetDifferences().
From there, instead of printing the differences into a Migration class, I want to create the MigrationCommands directly and apply those commands to my database.
The above sounds fairly straightforward but I'm having a lot of issues with the Dependency Injection system.
This is the code I have right now:
static DbContextOptions GetOptions(IModel model)
{
var builder = new DbContextOptionsBuilder();
builder
.UseSqlServer(connStr)
.UseModel(model);
return builder.Options;
}
class Test1ModelAEntity
{
public int Id { get; set; }
public string StrProp { get; set; }
}
static void Main(string[] args)
{
var sqlServerServices = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.BuildServiceProvider();
var conventions = new ConventionSet();
sqlServerServices.GetRequiredService<IConventionSetBuilder>().AddConventions(conventions);
var emptyModelBuilder = new ModelBuilder(conventions);
var emptyModel = emptyModelBuilder.Model;
var test1ModelBuilder = new ModelBuilder(conventions);
test1ModelBuilder.Entity<Test1ModelAEntity>()
.ToTable("ModelA");
var test1Model = test1ModelBuilder.Model;
using (TestContext ctx = new TestContext(GetOptions(emptyModel)))
{
var migrationServices = new ServiceCollection()
.AddDbContextDesignTimeServices(ctx)
.AddEntityFrameworkSqlServer()
.BuildServiceProvider();
var operations = migrationServices.GetRequiredService<IMigrationsModelDiffer>().GetDifferences(emptyModel, test1Model);
var commands = migrationServices.GetRequiredService<IMigrationsSqlGenerator>().Generate(operations, test1Model);
var connection = migrationServices.GetRequiredService<IRelationalConnection>();
migrationServices.GetRequiredService<IMigrationCommandExecutor>().ExecuteNonQuery(commands, connection);
}
}
This code throws a NullReferenceException with this stack trace:
at Microsoft.EntityFrameworkCore.Metadata.Internal.TableMapping.<>c.<GetRootType>b__10_0(IEntityType t)
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(TableMapping target)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<Add>d__37.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<DiffCollection>d__73`1.MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Sandbox.Program.Main(String[] args) in D:\Sandbox\Program.cs:line 108
I have inspected the source code and it appears that there's an issue with the way EFCore is interpreting my model. I am using EFCore version 2.1 preview 2.
Really I'm mostly trying random configurations on my IServiceCollections because I have no idea how to set this up. I am also trying to stay away from EFCore internal classes but if needed be I may use one or two for the time being.
Is there a way to take advantage of EFCore's built-in capabilities to generate some SQL given a pair of IModels? If so, how do I set up DI to have all the required services?
Thank you for the comments which pointed me in the correct direction.
In summary, I was trying to create my models using an empty convention set. This obviously leads to all sorts of problems as you have to generate the entire model explicitly, which is very complex.
To use the expected convention set I had to get it from my context using ConventionSet.CreateConventionSet. I also had to manually validate my model before being able to use it in queries and insert commands. The rest of the logic is pretty much the same.
Here's my final code including the tests I ran to ensure everything worked as expected:
static DbContextOptions GetOptions(IModel model)
{
var builder = new DbContextOptionsBuilder();
builder
.UseSqlServer(connStr)
.UseModel(model);
return builder.Options;
}
//Test 1
class Test1EntityA
{
public int Id { get; set; }
public string StrProp { get; set; }
}
//Test 2
class Test2EntityA
{
public int Id { get; set; }
public string StrProp { get; set; }
public ICollection<Test2ModelBEntity> Children { get; set; }
}
class Test2EntityB
{
public int Id { get; set; }
public int EntityAId { get; set; }
public Test2EntityA EntityA { get; set; }
}
static void Main(string[] args)
{
var emptyModelBuilder = new ModelBuilder(new ConventionSet());
var emptyModel = emptyModelBuilder.Model;
using (var baseCtx = new TestContext(GetOptions(emptyModel)))
{
//Get all services we need from the base context
var conventions = ConventionSet.CreateConventionSet(baseCtx);
var migrationServices = new ServiceCollection()
.AddDbContextDesignTimeServices(baseCtx)
.AddEntityFrameworkSqlServer()
.BuildServiceProvider();
//Test 1
var test1ModelBuilder = new ModelBuilder(conventions);
test1ModelBuilder.Entity<Test1EntityA>()
.ToTable("EntityA");
var test1Model = test1ModelBuilder.GetInfrastructure().Metadata;
test1Model.Validate();
var operations = migrationServices.GetRequiredService<IMigrationsModelDiffer>().GetDifferences(emptyModel, test1Model);
var commands = migrationServices.GetRequiredService<IMigrationsSqlGenerator>().Generate(operations, test1Model);
var connection = migrationServices.GetRequiredService<IRelationalConnection>();
migrationServices.GetRequiredService<IMigrationCommandExecutor>().ExecuteNonQuery(commands, connection);
using (TestContext ctx = new TestContext(GetOptions(test1Model)))
{
ctx.Set<Test1EntityA>().Add(new Test1EntityA
{
StrProp = "test1",
});
ctx.SaveChanges();
}
//Test 2
var test2ModelBuilder = new ModelBuilder(conventions);
test2ModelBuilder.Entity<Test2EntityA>()
.ToTable("EntityA");
test2ModelBuilder.Entity<Test2EntityB>()
.ToTable("EntityB");
var test2Model = test2ModelBuilder.GetInfrastructure().Metadata;
test2Model.Validate();
operations = migrationServices.GetRequiredService<IMigrationsModelDiffer>().GetDifferences(test1Model, test2Model);
commands = migrationServices.GetRequiredService<IMigrationsSqlGenerator>().Generate(operations, test2Model);
migrationServices.GetRequiredService<IMigrationCommandExecutor>().ExecuteNonQuery(commands, connection);
using (TestContext ctx = new TestContext(GetOptions(test2Model)))
{
var e = new Test2EntityA
{
StrProp = "test2",
};
ctx.Set<Test2EntityA>().Add(e);
ctx.Set<Test2EntityB>().Add(new Test2EntityB
{
EntityA = e,
});
ctx.SaveChanges();
Console.WriteLine(ctx.Set<Test2EntityB>().Where(b => b.EntityA.StrProp == "test2").Count());
}
}
}
I have this class Cart_Record, shown below. I want to update the PrimaryKey. To do that I am trying to clone the object into a new object to copy CartLines and update ID. I haven't found much in the issue queue or the documentation to help me.
public class Cart_Record : RealmObject
{
public Cart_Record() { }
public Cart_Record(IList<Cart_Line> cartLines, int id)
{
ID = id;
foreach (var cartLine in cartLines)
CartLines.Add(App.RealmDB.Find<Cart_Line>(cartLine.ProductId));
}
[PrimaryKey]
public int ID { get; set; }
public IList<Cart_Line> CartLines { get; }
}
I am trying this
var appCart = App.RealmDB.All<Cart_Record>().First();
App.RealmDB.Write(() =>
{
var cartLines = new List<Cart_Line>(appCart.CartLines);
App.RealmDB.Remove(App.RealmDB.Find<Cart_Record>(appCart.ID));
App.RealmDB.Add<Cart_Record>(new Cart_Record(cartLines, serverCart.ID));
});
However I keep getting exceptions, specifically RealmObjectManagedByAnotherRealmException. I don't understand how as I am not readding the Cart_Line objects to Realm, just to the CartLine list in the new object.
What am I doing wrong?
Thanks ahead of time.
Edit: I found something that works but I would like to see if someone else have a better method. This is what works for me.
var appCart = App.RealmDB.All<Cart_Record>().First();
App.RealmDB.Write(() =>
{
var cartLines = new List<Cart_Line>(appCart.CartLines);
App.RealmDB.Remove(App.RealmDB.Find<Cart_Record>(appCart.ID));
var newAppCart = App.RealmDB.Add<Cart_Record>(new Cart_Record() { ID = serverCart.ID });
foreach (var cartLine in cartLines)
newAppCart.CartLines.Add(cartLine);
});
I'm not sure what App.RealmDB does under the hood, but using the out-of-the-box Realm API, what you want to achieve can be done by simply adding the CartLines from the original to the updated object:
// Assume want to change Id from 1 to 2
var realm = Realm.GetInstance();
var original = realm.Find<Cart_Record>(1);
var updated = new Cart_Record { ID = 2 }; // other properties must be copied here
foreach (var cart in original.CartLines)
{
updated.CartLines.Add(cart);
}
realm.Write(() =>
{
realm.Remove(original);
realm.Add(updated);
});
// updated now has all the original's CartLines
I'm new in c# and write this class:
public class MANAGER
{
public string VersionName { get; set; }
public Double Value { get; set; }
}
and use this:
List<MANAGER> lstSummary = new List<MANAGER>();
MANAGER summary = new MANAGER();
summary.VersionName = "12";
summary.Value =12;
lstSummary.Add(summary);
summary.VersionName = "13";
summary.Value = 19;
lstSummary.Add(summary);
but up code just save latest class record ,means just save VersionName=13 and Value=19,How can i solve that problem?thanks.
You have to re-instantiate the summary variable like so:
MANAGER summary = new MANAGER();
summary.VersionName = "12";
summary.Value =12;
lstSummary.Add(summary);
summary = new MANAGER(); // <-- add this
summary.VersionName = "13";
summary.Value = 19;
lstSummary.Add(summary);
The problem as described in another answer is that you add the same object to the list multiple times, so you need to have a separate one. You can do it that way or don't use explicit the variable at all, since C# allows initializers. It will be pretty much the same logic, but a bit more nice and readable.
var lstSummary = new List<MANAGER>
{
new Manager
{
VersionName = "12",
Value = 12
},
new Manager
{
VersionName = "13",
Value = 19
}
}
I have issue in the following code. Below is my model code
public class Comments
{
public string displayComments { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? dTime { get; set; }
public int airPortId { get; set; }
}
public class LstComments
{
private List<Comments> _lstcomment = new List<Comments>();
public List<Comments> lstCommet
{
get
{
return _lstcomment;
}
set
{
_lstcomment = value;
}
}
}
and in mycontroller am getting data from EF and adding it to the properties in For loop. Code Below
Comments com = new Comments();
LstComments savedComments = new LstComments();
AirportEntities airPortEntity = new AirportEntities();
var userComments = from c in airPortEntity.AirportComments
select c;
//List<Comments> savedComments = new List<Comments>();
foreach (var item in userComments)
{
com.displayComments = item.Comments;
com.dTime = item.Time;
savedComments.lstCommet.Add(com);
}
My issue is my entire list is getting updated with same records(recently added data)
For eg. foreach 3rd timn updates both 1st and 2nd 3rd item in list with 3rd item data.
What i am doing wrong ?
You instantiate Comments outside of the loop. This means there are a bunch of references to the same comment object on the heap. You need to do
Comments com = new Comments(); inside of the foreach. This will create a separate instance on each iteration, instead of just giving the one instance new values.
you need to instantiate Comments com = new Comments(); each time in foreach. As for now you just rewrite reference to the same object.
Or which is better to rewrite foreach as:
foreach (var item in userComments)
{
savedComments.lstCommet.Add(
new Comments()
{
com.displayComments = item.Comments,
com.dTime = item.Time
});
}
I'm having some issues reading the properties of an item I have placed into an arraylist and I can't find the answer anywhere.
ArrayList itemsArrayList = new ArrayList();
itemsArrayList.Add(abyssalScepter);
itemsArrayList.Add(aegisOfTheLegion);
itemInBuildAbilityPower = itemsArrayList[0].abilityPower;
I need to be able to read the properties of the objects in the array so I can apply their values elsewhere but this gets me nowhere.
You need to cast object to the expected type (and hope it's really of this type).
itemInBuildAbilityPower = ((Item)itemsArrayList[0]).abilityPower;
The better option (if the infrastructure code is yours) to use generic container, e.g. List<T>.
List<Item> itemsArrayList = new List<Item>
itemsArrayList.Add(abyssalScepter);
itemsArrayList.Add(aegisOfTheLegion);
itemInBuildAbilityPower = itemsArrayList[0].abilityPower;
try
var itemInBuildAbilityPower = itemsArrayList[0].GetType().GetProperty ("abilityPower").GetGetMethod().Invoke (itemsArrayList[0], null);
Building on elder_george's answer, here is an example of what you could do if abyssalScepter and aegisOfTheLegion are not the exact same type:
using System.Collections.Generic;
class Power { }
interface IAbilityPower { Power abilityPower { get; set; } }
class Scepter : IAbilityPower { public Power abilityPower { get; set; } }
class Aegis : IAbilityPower { public Power abilityPower { get; set; } }
class Test
{
public static void Main()
{
var abyssalScepter = new Scepter();
var aegisOfTheLegion = new Aegis();
var itemsList = new List<IAbilityPower>();
itemsList.Add(abyssalScepter);
itemsList.Add(aegisOfTheLegion);
var power = itemsList[0].abilityPower;
}
}