Code-First Primary Key Violation - ef-code-first

I'm new to code-first, but love it. I have an inheritance structure: the nodes are coupled by a connection class. However, I suspect due to the PK, FK relationship between the base and derived classes there is is primary key violation.
Violation of PRIMARY KEY constraint 'PK_dbo.Connections'. Cannot insert duplicate key in object 'dbo.Connections'
So, not being a database Mack-O-Grady, I'm wondering if this is doable?
public abstract class Node
{
public Guid Id { get; set; }
}
[Table("Node1")]
public class Node1 : Node { }
[Table("Node2")]
public class Node2 : Node { }
[Table("Node3")]
public class Node3 : Node { }
public class Connection
{
public Guid Id { get; set; }
public Node Parent { get; set; }
public int ParentPort { get; set; }
public Node Child { get; set; }
}
public class TestContext : DbContext
{
public TestContext() : base("TST")
{
var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
}
public DbSet<Node> Nodes { get; set; }
public DbSet<Connection> Connections { get; set; }
}
class Program
{
static void Main(string[] args)
{
var nodes = new List<Node>();
using (var context = new TestContext())
{
var n1 = new Node1
{
Id = Guid.NewGuid(),
};
nodes.Add(n1);
context.Nodes.Add(n1);
var n2 = new Node2
{
Id = Guid.NewGuid(),
};
nodes.Add(n2);
context.Nodes.Add(n2);
var n3 = new Node2
{
Id = Guid.NewGuid(),
};
nodes.Add(n3);
context.Nodes.Add(n3);
var c1 = new Connection { Parent = n1, ParentPort = 0, Child = n2 };
context.Connections.Add(c1);
var c2 = new Connection { Parent = n2, ParentPort = 0, Child = n3 };
context.Connections.Add(c2);
context.SaveChanges(); // exception when saving????
Console.ReadLine();
}
}
}

The answer is because the Id property (primary key wasn't being set)
var c1 = new Connection { Id = Guid.NewGuid(), Parent = n1, ParentPort = 0, Child = n2 };
context.Connections.Add(c1);
var c2 = new Connection { Id = Guid.NewGuid(), Parent = n2, ParentPort = 0, Child = n3 };
context.Connections.Add(c2);

Related

“How to fix ‘The instance of entity type ' cannot be tracked because another instance with the key value '{TypeId: 1}' is already being tracked.

The instance of entity type 'WalletType' cannot be tracked because another instance with the key value '{TypeId: 1}' is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached.
//WalletType.cs
public class WalletType
{
public WalletType()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int TypeId { get; set; }
[MaxLength(150)]
public string TypeTitle { get; set; }
public virtual ICollection<Wallet> Wallets { get; set; }
}
////////////////////////////////
//SeedData.cs
public class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new ApplicationDbContext(
serviceProvider.GetRequiredService>()))
{
// Look for any movies.
if (context.WalletTypes.Any())
{
return; // DB has been seeded
}
context.WalletTypes.AddRange(
new WalletType
{
TypeId = 1,
TypeTitle = "function1"
},
new WalletType
{
TypeId = 1,
TypeTitle = "function2"
}
);
context.SaveChanges();
}
}
}
///////////////////////////////////////
//Program.cs
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.
GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
SeedData.Initialize(services);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
That's because you add to WalletType with the dame TypeId.
You can either set identity to auto or provide unique value manually.
context.WalletTypes.AddRange(
new WalletType
{
TypeId = 1,
TypeTitle = "function1"
},
new WalletType
{
TypeId = 2,
TypeTitle = "function2"
}
);

Deserializing arrayList to invalid JSON [duplicate]

Hi everyone i have a problem with my json serealization. I'm using the Json.NET package under Unity: I'm searching to make a Database, editable on my application and stored on my server through wwwForm and a php file. I have no problem to create it and to push it on the net. The problem is, when i load it, the database has a new entry at the end. The Database Class is this:
public class Database {
public List<Army> armies { get; set;}
public Database() {
armies = new List<Army>();
armies.Add (new Army());
}
}
public class Army
{
public string faction { get; set;}
public List<Unit> army { get; set;}
public Army()
{
faction = "RED";
army = new List<Unit>();
army.Add (new Unit());
}
}
public class Unit
{
public string name { get; set;}
public float fissionTimer { get; set;}
public float HP { get; set;}
public int shield { get; set;}
public float strenght { get; set;}
public float movSpeed { get; set;}
public float attackSpeed { get; set;}
public float farmAggro { get; set;}
public int criticPossibility { get; set;}
public int armorPenetration { get; set;}
public bool isContagious { get; set;}
public int contagePossibility { get; set;}
public string imgName {get;set;}
public Unit()
{
name = "standard";
fissionTimer = 8;
HP = 100;
shield = 0;
strenght = 10;
movSpeed = 5;
attackSpeed = 0.1f;
farmAggro = 0.1f;
criticPossibility = 0;
armorPenetration = 0;
isContagious = false;
contagePossibility = 0;
imgName = "Red";
}
public Unit(string _name, float _fissionTimer, float _HP, int _shield, float _strenght, float _movSpeed, float _attackSpeed,
float _farmAggro, int _criticPossibility, int _armorPen, bool _iscontagious, int _contagePos, string _imgName)
{
name = _name;
fissionTimer = _fissionTimer;
HP = _HP;
shield = _shield;
strenght = _strenght;
movSpeed = _movSpeed;
attackSpeed = _attackSpeed;
farmAggro = _farmAggro;
criticPossibility = _criticPossibility;
armorPenetration = _armorPen;
isContagious = _iscontagious;
contagePossibility = _contagePos;
imgName = _imgName;
}
}
to serialize and deserialize i use those 2 methods:
IEnumerator LoadFile()
{
WWW www = new WWW(dbPath);
yield return www;
var _database = JsonConvert.DeserializeObject<Database> (www.text);
db = _database;
SendMessage ("loaded", SendMessageOptions.DontRequireReceiver);
}
IEnumerator SaveFile(Database db)
{
WWWForm form = new WWWForm();
string serialized = JsonConvert.SerializeObject (db);
form.AddField("theDatabase", serialized);
WWW www = new WWW(phpPath, form);
yield return www;
if (www.error == null)
Debug.Log ("saved" + serialized);
else
Debug.LogError ("error saving database");
}
The result of using the default constructor, serialized and deserialized is this:
{
"armies": [
{
"faction": "RED",
"army": [
{
"name": "standard",
"fissionTimer": 8,
"HP": 100,
"shield": 0,
"strenght": 10,
"movSpeed": 5,
"attackSpeed": 0.1,
"farmAggro": 0.1,
"criticPossibility": 0,
"armorPenetration": 0,
"isContagious": false,
"contagePossibility": 0,
"imgName": "Red"
}
]
},
{
"faction": "RED",
"army": [
{
"name": "standard",
"fissionTimer": 8,
"HP": 100,
"shield": 0,
"strenght": 10,
"movSpeed": 5,
"attackSpeed": 0.1,
"farmAggro": 0.1,
"criticPossibility": 0,
"armorPenetration": 0,
"isContagious": false,
"contagePossibility": 0,
"imgName": "Red"
}
]
}
]
}
There are 2 armies and 2 units. Where i am doing wrong? Thanks in advance
The reason this is happening is due to the combination of two things:
Your class constructors automatically add default items to their respective lists. Json.Net calls those same constructors to create the object instances during deserialization.
Json.Net's default behavior is to reuse (i.e. add to) existing lists during deserialization instead of replacing them.
To fix this, you can either change your code such that your constructors do not automatically add default items to your lists, or you can configure Json.Net to replace the lists on deserialization rather than reusing them. The latter by can be done by changing the ObjectCreationHandling setting to Replace as shown below:
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ObjectCreationHandling = ObjectCreationHandling.Replace;
var database = JsonConvert.DeserializeObject<Database>(www.text, settings);
Best way would be to configure JSON.Net to replace the default values by
JsonSerializerSettings jsSettings = new JsonSerializerSettings
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
};
JsonConvert.DeserializeObject<Army>(jsonString, jsSettings);

Web API & entitiy framework how to work with posted json array

I try to write a post method for my asp.net web API.
The method shoulde be
1. receive Json formated datastring with have single objects and one sub array.
2. write it in my database in two tables with have a 1:n relation.
It would be wonderfull if anyone can help me.
I've no more idea how i can realise it
Example of the Json data:
[
{
"User":"testuser",
"CPGRP":21321321,
"Sex":"men",
"Name": "test",
"PointList":[
{
"Cost_I_Rea":"22202771.01",
"Cost_TV":"213213210.0" ,
"GRP":10,
"ID":0,
"ReichweiteID_F":1,
"RW_TV":"9.603",
"RW_Zuwgs":"9.603",
},
{
"Cost_I_Rea":"22202771.01",
"Cost_TV":"213213210.0" ,
"GRP":10,
"ID":0,
"ReichweiteID_F":1,
"RW_TV":"9.61103",
"RW_Zuwgs":"9.6043",
}
]
"Potenzial":213213,
"ReichweiteID":0,
"ZielGRP":21321321
}
]
This is my Post Method now, but it's generate a 500 Error:
// POST api/TVKurve
[ResponseType(typeof(Points))]
public async Task<IHttpActionResult> PostPoints(Points points)
{
//if (!ModelState.IsValid)
//{
// return BadRequest(ModelState);
//}
db.Points.Add(points);
await db.SaveChangesAsync();
db.Entry(points).Reference(x => x.Reichweite).Load();
var dto = new ReichweitePointsDto()
{
ReichweiteID = points.ReichweiteId,
Sex = points.Reichweite.Geschlecht,
Name = points.Reichweite.Name,
CPGRP = points.Reichweite.CpGRP,
Potenzial = points.Reichweite.Potenzial,
ZielGRP = points.Reichweite.ZielGRP,
User = points.Reichweite.Benutzer,
PointList = new List<PointListDto>(),
};
return CreatedAtRoute("DefaultApi", new { id = points.Id }, points);
}
and my data annotation Model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace xms_ef_vers1.Models
{
public class PointListDto
{
public PointListDto() { }
public PointListDto(Points item)
{
ID = item.Id;
GRP = item.GRP;
RW_TV = item.RW_TV;
Cost_TV = item.Cost_TV;
RW_Zuwgs = item.Rw_ZuWGS;
Cost_I_Rea = item.Cost_I_Rea;
ReichweiteID_F = item.ReichweiteId;
}
[Key]
public int ID { get; set;}
[Required]
public int GRP { get; set; }
public decimal RW_TV { get; set; }
public double Cost_TV { get; set; }
public decimal RW_Zuwgs { get; set; }
public decimal Cost_I_Rea { get; set; }
public int ReichweiteID_F { get; set; }
public Points ToEntity()
{
return new Points
{
Id = ID,
GRP = GRP,
RW_TV = RW_TV,
Cost_TV = Cost_TV,
Rw_ZuWGS = RW_Zuwgs,
Cost_I_Rea = Cost_I_Rea,
ReichweiteId = ReichweiteID_F,
};
}
}
}
Thank you for your answer.
I ve got it.
Look here:
public HttpResponseMessage PostReicheite(ReichweitePointsDto reichw)
{
if (ModelState.IsValid)
{
var reichwe = new Reichweite()
{
Geschlecht = reichw.Geschlecht,
Name = reichw.Name,
CpGRP = reichw.CPGRP,
Potenzial = reichw.Potenzial,
ZielGRP = reichw.ZielGRP,
Benutzer = reichw.Benutzer,
PointListe = (from item in reichw.PointListe
select new Points()
{
GRP = item.GRP,
RW_TV = item.RW_TV,
Cost_TV = item.Cost_TV,
Rw_ZuWGS = item.RW_Zuwgs,
Cost_I_Rea = item.Cost_I_Rea,
}).ToList()
};
db.Reichweites.Add(reichwe);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, reichwe);
response.Headers.Location = new Uri(Url.Link("DefaultAPI", new { id = reichwe.Id }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
Now the Post Method do exactly what i need.
Only one bug, i've got an 500 status back can you say me why?
Best

MVC3 Session State

I've recently been re-factoring my code to move from inproc to Session State. I've created a new class that I built from the start to be serializable. I'm currently getting the 'Unable to serialize the session state.' error. However, I am able to serialize the class using JsonConvert (newtonsoft).
Are methods not allowed or something?
This is what it looks like:
[Serializable()]
public class SessionStateObject
{
public int CurrentSessionId { get; set; }
public int CurrentAssessmentId { get; set; }
public int CurrentAssessmentElementId { get; set; }
public int CurrentUserId { get; set; }
public Dictionary<string, int> AssessmentItemsIds { get; set; }
public Dictionary<string, int> SessionResponseIds { get; set; }
public List<string> AdministeredItemOrderByName;
public string LastVisitedItem;
public int? TotalAssessmentCurrentItemOrder;
public RenderablePersistanceObject RenderablePersistance;
#region formula engine section variables
public string BranchingResult { get; set; }
public string ContentOutput { get; set; }
public int CurrentItemId { get; set; }
public int VirtualWorkingItemId { get; set; }
public bool isCloneItem { get; set; }
public bool wasPreFormulaRun;
public bool wasContentOutput;
public bool wasPrefillReached;
public bool isVirtual;
public List<string> FormulaStack { get; set; }
public List<string> ItemNameTokens { get; set; }
public string serializedJSContext { get; set; }
public string CloneSourceItem { get; set; }
public int itemsAbletoFastForwardThrough { get; set; }
#endregion
private Dictionary<int, int> itemIdByMultiGroupId; //key is itemId, val is groupId
private Dictionary<int, List<int>> MultiGroupIdByItemIds; //key is groupId, val is itemId
public SessionStateObject()
{
RenderablePersistance = new RenderablePersistanceObject();
AssessmentItemsIds = new Dictionary<string, int>();
SessionResponseIds = new Dictionary<string, int>();
AdministeredItemOrderByName = new List<string>();
FormulaStack = new List<string>();
ItemNameTokens = new List<string>();
itemIdByMultiGroupId = new Dictionary<int, int>();
MultiGroupIdByItemIds = new Dictionary<int, List<int>>();
}
public void initMultiItemGroups(Assessment assessment, NetScidDbContainer dbContext)
{
List<MultiItem> assessmentMultiItems = (from multi in dbContext.MultiItems
where multi.AssessmentId == assessment.Id
select multi).ToList();
List<int> uniqueGroupIds = new List<int>();
foreach (MultiItem mItem in assessmentMultiItems)
{
itemIdByMultiGroupId.Add(mItem.ItemId, mItem.GroupId);
if (!uniqueGroupIds.Contains(mItem.GroupId))
{
uniqueGroupIds.Add(mItem.GroupId);
}
}
foreach (int groupId in uniqueGroupIds)
{
List<int> ItemIds = (from itemGroup in assessmentMultiItems
where itemGroup.GroupId == groupId
orderby itemGroup.Id ascending
select itemGroup.ItemId).ToList();
MultiGroupIdByItemIds.Add(groupId, ItemIds);
}
}
public List<int> GetItemIdsFromSingleItemId(int itemId)
{
List<int> foundItemIDs = new List<int>();
int foundGroupId = -1;
if (this.itemIdByMultiGroupId.ContainsKey(itemId))
{
foundGroupId = this.itemIdByMultiGroupId[itemId];
}
if (this.MultiGroupIdByItemIds.ContainsKey(foundGroupId))
{
foundItemIDs = this.MultiGroupIdByItemIds[foundGroupId];
}
return foundItemIDs;
}
public void nullifyRenderable()
{
this.RenderablePersistance = null;
}
public void PersistRenderable(IRenderable renderable)
{
this.RenderablePersistance = new RenderablePersistanceObject();
if (renderable is MultiItemRenderable)
{
//get list of item IDs from the multi-item lookup
this.RenderablePersistance.isMultiItem = true;
this.RenderablePersistance.primaryItemId = ((Item)((MultiItemRenderable)renderable).IndexedItems.Last()).Id;
}
else //regular renderable
{
this.RenderablePersistance.isMultiItem = false;
this.RenderablePersistance.primaryItemId = ((Item)renderable).Id;
}
}
public AssessmentRuntime StartAdministrativeSession(NetScidDbContainer dataContext, Assessment assessment, User currentUser, string pid = "1")
{
AssessmentRuntime newRuntime = new AssessmentRuntime(this, dataContext);
Session newSession = new Session();
assessment.PrepElements();
PermissionEntity rootPE = new PermissionEntity();
if (currentUser != null)
{
rootPE = PermissionEntityHelper.GetRootPermissionEnity(dataContext, currentUser);
}
else
{
rootPE = (from adminpe in dataContext.PermissionEntities
where adminpe.Name == "TelesageAdmin"
select adminpe
).FirstOrDefault();
}
newSession.Participant = (from pids in dataContext.ParticipantIdAliasLookups
where pid == pids.AliasId && pids.RootPermissionEntity.Id == rootPE.Id
select pids.Participant).FirstOrDefault();
if (newSession.Participant == null)
{
Participant newParticipant = new Participant();
ParticipantIdAliasLookup newPidAlias = new ParticipantIdAliasLookup();
newParticipant.ParticipantDataJSON = "";
newPidAlias.AliasId = pid;
newPidAlias.RootPermissionEntity = rootPE;
newParticipant.AliasLookup = newPidAlias;
newParticipant.PermissionEntities.Add(currentUser.PermissionEntity);
newParticipant.PermissionEntities.Add(rootPE);
newSession.Participant = newParticipant;
dataContext.Participants.AddObject(newParticipant);
dataContext.ParticipantIdAliasLookups.AddObject(newPidAlias);
}
newSession.Assessment = assessment;
newSession.User = currentUser;
newSession.StartTime = DateTime.Now;
newSession.PermissionEntity = currentUser.PermissionEntity;
newSession.SetSessionData("engineversion", typeof(SmartQWeb.MvcApplication).Assembly.GetName().Version.ToString());
newSession.SetSessionData("assessmentname", newSession.Assessment.Name);
newSession.SetSessionData("assessmentversion", newSession.Assessment.Version);
newSession.SetSessionData("computername", Environment.MachineName);
newSession.SetSessionData("username", currentUser.Name);
dataContext.Sessions.AddObject(newSession);
newSession.SetSessionData("dxdata", JsonConvert.SerializeObject(newRuntime.RenderedDiagnoses));
newRuntime.formulaEngine = new FixedLengthFormulaEngine(newRuntime);
SessionLog newSessionLog = new SessionLog();
dataContext.SessionLogs.AddObject(newSessionLog);
newSession.SessionLog = newSessionLog;
dataContext.SaveChanges();
initMultiItemGroups(assessment, dataContext);
this.CurrentSessionId = newSession.Id;
this.CurrentUserId = currentUser.Id;
this.CurrentAssessmentId = assessment.Id;
newRuntime.Context = new RuntimeContext(this, dataContext);
this.GetAssessmentItems(assessment); //to populate the items dict
newRuntime.formulaEngine.InitializeContext(this.AssessmentItemsIds.Keys.ToList());
newRuntime.RenderedDiagnoses = new RenderedDiagnosisModel(newRuntime.Context.AdministeredItemOrderByName);
newRuntime.Context.Logger.WriteLog("Session started with assessment: " + newRuntime.Context.Assessment.Name + " with version: " + newRuntime.Context.Assessment.Version);
newRuntime.Context.Logger.WriteLog("Session started by user: " + newRuntime.Context.CurrentUser.Name + "for participant ID: " + newSession.ParticipantId);
return newRuntime;
}
//start from a previous existing session
public AssessmentRuntime StartAdministrativeSession(NetScidDbContainer dataContext, Assessment assessment, Session previousSession, User currentUser, string pid = "", string resumefromlatest = "false")
{
AssessmentRuntime newRuntime = new AssessmentRuntime(this, dataContext);
Session newSession = new Session();
Assessment sessionAssessment = assessment;
newSession.ParticipantId = previousSession.ParticipantId;
//THE OTHER ENTITIES BESIDES THE SESSION NEED TO BE DETATCHED AND RE-ATTACHED (BY BEING ADDED TO THE NEW SESSION)
assessment.PrepElements();
newRuntime.RenderedDiagnoses = new RenderedDiagnosisModel(newRuntime.Context.AdministeredItemOrderByName);
List<Response> prevresponses = previousSession.Responses.ToList();
if (sessionAssessment == assessment)
{
foreach (Response prevresponse in prevresponses)
{
newRuntime.Context.CachedAssessmentResponses.Add(prevresponse.Item.ItemName, prevresponse);
dataContext.Detach(prevresponse);
newSession.Responses.Add(prevresponse);
}
}
else
{
//the sessionAssessment is now the more up-to-date one so the responses pulled will have to be mapped, not just detatched and copied
foreach (Response prevresponse in prevresponses)
{
Dictionary<string, FixedLengthItem> newAssessmentItemNames = AssessmentHelper.GetAssessmentItems(sessionAssessment);
if (!newAssessmentItemNames.ContainsKey(prevresponse.Item.ItemName))
continue;
Response newResponse = new Response
{
NumericValue = prevresponse.NumericValue,
Item = newAssessmentItemNames[prevresponse.Item.ItemName],
ItemName = prevresponse.Item.ItemName,
AdministeredOrder = -1,
Value = prevresponse.Value
};
newRuntime.Context.CachedAssessmentResponses.Add(newResponse.Item.ItemName, newResponse);
newSession.Responses.Add(newResponse);
}
}
newSession.SessionDataJSON = previousSession.SessionDataJSON;
//DxData
newRuntime.RenderedDiagnoses =
JsonConvert.DeserializeObject<RenderedDiagnosisModel>(newSession.GetSessionData("dxdata"));
//inc session is =2 when the the session has been resumed by
previousSession.IncSession = 2;
newSession.SetSessionData("previoussession", previousSession.Id.ToString());
newSession.Participant = previousSession.Participant;
newSession.Assessment = sessionAssessment;
newSession.User = currentUser;
newSession.PermissionEntity = currentUser.PermissionEntity;
newSession.StartTime = DateTime.Now;
dataContext.Sessions.AddObject(newSession);
dataContext.SaveChanges();
newRuntime.formulaEngine = new FixedLengthFormulaEngine(newRuntime);
initMultiItemGroups(assessment, dataContext);
newSession.SetSessionData("continuedsession", newSession.Id.ToString());
this.CurrentSessionId = newSession.Id;
this.CurrentUserId = currentUser.Id;
this.CurrentAssessmentId = assessment.Id;
this.GetAssessmentItems(assessment); //to populate the items dict
this.SetUpPreviousResponses(newSession); //populates SSO responses
newRuntime.Context = new RuntimeContext(this, dataContext);
if (newRuntime.RenderedDiagnoses != null)
newRuntime.RenderedDiagnoses.reInitAdminOrder(newRuntime.Context.AdministeredItemOrderByName);
newRuntime.formulaEngine.InitializeContext(this.AssessmentItemsIds.Keys.ToList());
return newRuntime;
}
//start from the SSO
public AssessmentRuntime ResumeSessionFromState(NetScidDbContainer dataContext)
{
AssessmentRuntime newRuntime = new AssessmentRuntime(this, dataContext);
newRuntime.formulaEngine = new FixedLengthFormulaEngine(newRuntime);
newRuntime.formulaEngine.InitializeSerializedContext(serializedJSContext);
newRuntime.Context = new RuntimeContext(this, dataContext);
if (this.CurrentAssessmentElementId != 0)
{
newRuntime.CurrentAssessmentElement = (from ae in dataContext.AssessmentElements
where ae.Id == this.CurrentAssessmentElementId
select ae).FirstOrDefault();
newRuntime.CurrentAssessmentElement.GetNewRuntime(newRuntime.Context);
}
return newRuntime;
}
private void GetAssessmentItems(Assessment assessment)
{
var fixedLengthElements = (from elements in assessment.AssessmentElements
where elements is FixedLengthBlock
select elements);
foreach (FixedLengthBlock elemblock in fixedLengthElements)
{
foreach (FixedLengthItem item in elemblock.ItemBank.Items)
{
item.aeRef = elemblock;
AssessmentItemsIds.Add(item.ItemName, item.Id);
}
}
}
private void SetUpPreviousResponses(Session newSession)
{
//put response IDs by itemname into this dictionary
SessionResponseIds = (from response in newSession.Responses
select response).ToDictionary(key => key.ItemName, value => value.Id);
}
}
And the RenderablePersistanceObject looks like this:
public class RenderablePersistanceObject
{
public int primaryItemId;
public int MultiItemGroupId;
public bool isMultiItem;
}
You can't serialize a straight Dictionary like that. You can take a look at this MSDN article for suggestions on implementing. If you don't truly need a Dictionary, perhaps List> might work better?
As Erik pointed out, it was the lack of the adorner [Serializable()] on that other object that caused the problem. Adding it solved my issue.

Proper way to handle nullable fields in RavenDB Map/Reduces?

How should I Map an object with a nullable field? I guess I must turn the nullable field into a non-nullable version, and it's that step that I stumble upon.
What is the proper way to map nullable properties?
public class Visit {
public string Id { get; set; }
public int? MediaSourceId { get; set; }
}
public class MapReduceResult
{
public string VisitId { get; set; }
public int MediaSourceId { get; set; }
public string Version { get; set; }
public int Count { get; set; }
}
AddMap<Visit>(
visits =>
from visit in visits
select new
{
VisitId = visit.Id,
MediaSourceId =
(visit.MediaSourceId.HasValue)
? visit.MediaSourceId
: UNUSED_MEDIASOURCE_ID,
Version = (string) null,
Count = 1
});
This doesn't work! In fact; this Map is completely ignored, while the other Maps work fine, and they are in the end Reduced as expected.
Thanks for helping me!
Below is a newly added test case that fails with a "Cannot assign <null> to anonymous type property". How am I supposed to get this flying with the least amount of pain?
[TestFixture]
public class MyIndexTest
{
private IDocumentStore _documentStore;
[SetUp]
public void SetUp()
{
_documentStore = new EmbeddableDocumentStore {RunInMemory = true}.Initialize();
_documentStore.DatabaseCommands.DisableAllCaching();
IndexCreation.CreateIndexes(typeof (MyIndex).Assembly, _documentStore);
}
[TearDown]
public void TearDown()
{
_documentStore.Dispose();
}
[Test]
public void ShouldWork()
{
InitData();
IList<MyIndex.MapReduceResult> mapReduceResults = null;
using (var session = _documentStore.OpenSession())
{
mapReduceResults =
session.Query<MyIndex.MapReduceResult>(
MyIndex.INDEX_NAME)
.Customize(x => x.WaitForNonStaleResults()).ToArray();
}
Assert.That(mapReduceResults.Count, Is.EqualTo(1));
}
private void InitData()
{
var visitOne = new Visit
{
Id = "visits/64",
MetaData = new MetaData {CreatedDate = new DateTime(1975, 8, 6, 0, 14, 0)},
MediaSourceId = 1,
};
var visitPageVersionOne = new VisitPageVersion
{
Id = "VisitPageVersions/123",
MetaData = new MetaData {CreatedDate = new DateTime(1975, 8, 6, 0, 14, 0)},
VisitId = "visits/64",
Version = "1"
};
using (var session = _documentStore.OpenSession())
{
session.Store(visitOne);
session.Store(visitPageVersionOne);
session.SaveChanges();
}
}
public class MyIndex :
AbstractMultiMapIndexCreationTask
<MyIndex.MapReduceResult>
{
public const string INDEX_NAME = "MyIndex";
public override string IndexName
{
get { return INDEX_NAME; }
}
public class MapReduceResult
{
public string VisitId { get; set; }
public int? MediaSourceId { get; set; }
public string Version { get; set; }
public int Count { get; set; }
}
public MyIndex()
{
AddMap<Visit>(
visits =>
from visit in visits
select new
{
VisitId = visit.Id,
MediaSourceId = (int?) visit.MediaSourceId,
Version = (string) null,
Count = 1
});
AddMap<VisitPageVersion>(
visitPageVersions =>
from visitPageVersion in visitPageVersions
select new
{
VisitId = visitPageVersion.VisitId,
MediaSourceId = (int?) null,
Version = visitPageVersion.Version,
Count = 0
});
Reduce =
results =>
from result in results
group result by result.VisitId
into g
select
new
{
VisitId = g.Key,
MediaSourceId = (int?) g.Select(x => x.MediaSourceId).FirstOrDefault(),
Version = g.Select(x => x.Version).FirstOrDefault(),
Count = g.Sum(x => x.Count)
};
}
}
}
You don't need to do anything to give nullables special treatment.
RavenDB will already take care of that.

Resources