Can't add index via C# to RavenDB database - c#

I'm trying to create a simple index, which includes two fields from my document: TraceRowID, LoadDate.
So I've created the following class:
using Model;
using Raven.Abstractions.Indexing;
using Raven.Client.Indexes;
using Raven.Client.Linq;
using Raven.Client.Document;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Indexes
{
public class IDLoadDateIndex : AbstractIndexCreationTask<ServiceTrace>
{
public IDLoadDateIndex()
{
Map = serviceTrace => from st in serviceTrace
select new { ID = st.TraceRowID, LoadDate = st.LoadDate };
}
}
}
My model is "ServiceTrace":
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Model
{
public class ServiceTrace
{
public int TraceRowID { get; set; }
public string StatusDescription { get; set; }
public string FullExceptionText { get; set; }
public string LoadDate { get; set; }
}
}
And there goes my DAL:
using Indexes;
using Raven.Client.Indexes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL
{
public class DbConnection
{
private readonly static DbConnection instance = new DbConnection();
private Raven.Client.Document.DocumentStore documentStore;
static DbConnection()
{
}
private DbConnection()
{
this.documentStore = new Raven.Client.Document.DocumentStore { Url = "http://btadmins:8080/" };
this.documentStore.Initialize();
IndexCreation.CreateIndexes(typeof(IDLoadDateIndex).Assembly, this.documentStore);
}
public static DbConnection Instance
{
get
{
return instance;
}
}
public Raven.Client.Document.DocumentStore DocumentStore
{
get
{
return documentStore;
}
}
}
}
For some reason, when stepping over on debug mode on the "CreateIndexes" line, it is done succesfully. BUT I can't see any indexes added on the RavenDB management studio silverlight app.
What can be the problem ?

Perhaps you are looking in a named database in the studio? By not naming a specific database here, you are creating the index in Raven's system database.

Related

how to using FindAll to get List in c#

Hi Everyone sorry i have a question, i have a simple code to get cheaper books from a list, i use one class book with properties, one class to set the books to the list and one main program. here is the book class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvanceCSHarpLambda
{
public class Book
{
public string Title { get; set; }
public int Price { get; set; }
}
}
and here the BookRepository class (List)
using System;
using System.Collections.Generic;
namespace AdvanceCSHarpLambda
{
public class BookRepository
{
public List<Book> GetBooks()
{
return new List<Book>
{
new Book() {Title="T1", Price=1 },
new Book() {Title="T2", Price=2 },
new Book() {Title="T3", Price=3 }
};
}
}
}
and here the main program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdvanceCSHarpLambda
{
class Program
{
static void Main(string[] args)
{
var books = new BookRepository();
var cheapBooks = books.FindAll(bookCheaper);
foreach (var book in cheapBooks)
{
Console.WriteLine(book.Title);
}
}
static bool bookCheaper(Book book)
{
return book.Price < 10;
}
}
}
the problem is i got red line in books.FindAll(bookCheaper), any one can help me whats going on in this redline? thx before
You need to call GetBooks
books.GetBooks().FindAll(bookCheaper)
Your books should be a List, not BookRepository.
You need a Predicate for the FindAll function.
books.FindAll(x=>x.Price < 3)
Something like this.

Dynamic object doesn't contain a definition for a property from a reference

The error I got :
"object" doesn't contain a definition for "test"
I also tried game.test() but I keep getting this error
This solution is divided in two distinct projects :
The first one is a .dll
The second one is a console
the goal is to call the get method from the 'iw4mp' class dynamicaly. So I would be able to call any from the class while it will be loaded.
the COD class should look useless but in the futur it will look if the process is running on the computer but for my test I use a string (but it actually work same way as if it was looking for a process).
Code from the DLL
COD
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CODOffsets.Interface;
using CODOffsets.Offsets;
namespace CODOffsets
{
public class COD
{
static string[] games = { "iw4mp", "iw5mp", "bo1" };
static Type CallofDuty;
public static bool checkGame()
{
foreach (string game in games)
{
if (ProcessHandle(game))
{
CallofDuty = Type.GetType("CODOffsets.Offsets" + "." + game);
return true;
}
}
return false;
}
public static object Game()
{
return Activator.CreateInstance(CallofDuty) as ICODClass;
}
public static bool ProcessHandle(string game)
{
if (game == "iw4mp")
return true;
else
return false;
}
}
}
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CODOffsets.Interface
{
interface ICODClass
{
string test { get; }
}
}
Offset
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CODOffsets.Interface;
namespace CODOffsets.Offsets
{
class iw4mp : ICODClass
{
public string test { get { return "this is mw2"; } }
}
}
Code from the Console project
Main
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CODOffsets;
namespace TestGenericClass
{
class Program
{
static void Main(string[] args)
{
if (COD.checkGame())
{
dynamic game = COD.Game();
Console.WriteLine(game.test);
Console.ReadKey();
}
}
}
}
It should basically work as the way you did. But, if not so, here are some alternatives.
you can use reflections in c# to get all the properties of the dynamic object.
var nameOfProperty = "test";
var propertyInfo = game.GetType().GetProperty(nameOfProperty);
var value = propertyInfo.GetValue(game, null);
Moreover, you can simply use this way to get the value if you know the property name
string value = game["test"];

MEF2 Lightweight System.Composition with Metadata

The complete lack of examples for how to use lightweight MEF2, System.Composition, makes this tricky. I am only using System.Composition (not System.ComponentModel.Composition).
I want to import parts that have metadata. I'm using attributed code. Unfortunately when I try to get the exports I get a big fat null.
The MetadataAttribute:
namespace Test.ConfigurationReaders
{
using System;
using System.Composition;
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportReaderAttribute : ExportAttribute
{
public string Reader { get; set; }
}
}
Export:
namespace Test.ConfigurationReaders
{
using System.Collections.Generic;
using System.Configuration;
[ExportReader(Reader = "CONFIG")]
public class ConfigReader : IConfigurationReader
{
public IEnumerable<Feature> ReadAll()
{
return new List<Feature>();
}
}
}
ImportMany and filter on metadata:
namespace Test.Core
{
using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Hosting;
using System.Configuration;
using System.Linq;
using System.Reflection;
using ConfigurationReaders;
public sealed class Test
{
static Test()
{
new Test();
}
private Test()
{
SetImports();
Reader = SetReader();
}
[ImportMany]
private static IEnumerable<ExportFactory<IConfigurationReader, ExportReaderAttribute>> Readers { get; set; }
public static IConfigurationReader Reader { get; private set; }
private static IConfigurationReader SetReader()
{
// set the configuation reader based on an AppSetting.
var source =
ConfigurationManager.AppSettings["Source"]
?? "CONFIG";
var readers = Readers.ToList();
var reader =
Readers.ToList()
.Find(
f =>
f.Metadata.Reader.Equals(
source,
StringComparison.OrdinalIgnoreCase))
.CreateExport()
.Value;
return reader;
}
private void SetImports()
{
var configuration =
new ContainerConfiguration().WithAssemblies(
new List<Assembly> {
typeof(IConfigurationReader).Assembly });
var container = configuration.CreateContainer();
container.SatisfyImports(this);
Readers = container.GetExports<ExportFactory<IConfigurationReader, ExportReaderAttribute>>();
}
}
}
Readers is, unfortunately, null. This is example code here but I can see parts that don't have metadata in my actual code so that at least is working.
What should I do to populate Readers?
I'm trying to target .NET Standard 2.0 and use it from .NET Core.
The solution is to change the attributes for the exported class:
[Export(typeof(IConfigurationReader))]
[ExportMetadata("Reader", "CONFIG")]
public class ConfigReader : IConfigurationReader
{
}

"Object not set to an instance of an object" using code that works in one instance, does not work in another instance [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I am using VS'15 on asp.net 4.6.
I am also modeling my application after the N-Tier design found at:
Implementing a generic data access layer using Entity Framework
I am able to return data from a User table, however when using code based on the User code to return data from another table, I get a null return.
My BLL code that works:
using System;
using System.Collections.Generic;
using System.Linq;
using Library.DataAccessLayer;
using Library.Model;
namespace Library.BusinessLogicLayer
{
public interface IBusinessLogicLayer_User
{
IList<UsersModel> GetAllUsers();
UsersModel GetUserByAapNetUserID(string _UserID);
void UpdateUser(params UsersModel[] _UserModel);
}
public class BusinessLogicLayer_User : IBusinessLogicLayer_User
{
private readonly IUsersRepository _UsersRepository;
public BusinessLogicLayer_User()
{
_UsersRepository = new UsersRepository();
}
public BusinessLogicLayer_User(IUsersRepository userRepository)
{
_UsersRepository = userRepository;
}
public IList<UsersModel> GetAllUsers()
{
return _UsersRepository.GetAll();
}
public UsersModel GetUserByAapNetUserID(string _UserID)
{
return _UsersRepository.GetSingle(u => u.AspNetUserID.Equals(_UserID));
}
public void UpdateUser(params UsersModel[] _UsersModel)
{
_UsersRepository.Update(_UsersModel);
}
}
}
My BLL code that does not work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Library.DataAccessLayer;
using Library.Model;
namespace Library.BusinessLogicLayer
{
public interface IBusinessLogicLayer_AreaSecurity
{
AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID);
void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel);
IList<AreaSecurityModel> GetAllAreaSecurity();
}
public class BusinessLogicLayer_AreaSecurity : IBusinessLogicLayer_AreaSecurity
{
private readonly IAreaSecurityRepository _AreaSecurityRepository;
public BusinessLogicLayer_AreaSecurity()
{
_AreaSecurityRepository = new AreaSecurityRepository();
}
public BusinessLogicLayer_AreaSecurity(IAreaSecurityRepository areaSecurityRepository)
{
_AreaSecurityRepository = areaSecurityRepository;
}
public void AddAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
{
_AreaSecurityRepository.Add(_AreaSecurityModel);
}
public IList<AreaSecurityModel> GetAllAreaSecurity()
{
return _AreaSecurityRepository.GetAll();
}
public AreaSecurityModel GetAreaSecurityByUserID(string _AspNetUserID)
{
return _AreaSecurityRepository.GetSingle(r => r.AspNetUserID.Equals(_AspNetUserID));
}
public void UpdateAreaSecurity(params AreaSecurityModel[] _AreaSecurityModel)
{
_AreaSecurityRepository.Update(_AreaSecurityModel);
}
}
}
And here is the code that is calling the stuff that works and does not work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Library.BusinessLogicLayer;
using Library.Model;
namespace IdahoFalls9thWardBulletin.Controllers
{
public class ApplicationBaseController : Controller
{
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (User != null)
{
var context = new ApplicationDbContext();
var username = User.Identity.Name;
if (!string.IsNullOrEmpty(username))
{
var user = context.Users.SingleOrDefault(u => u.UserName == username);
BusinessLogicLayer_User _BusinessLogicLayer_User = new BusinessLogicLayer_User();
var UserDetails = _BusinessLogicLayer_User.GetUserByAapNetUserID(user.Id);
ViewData.Add("FirstName", UserDetails.FirstName);
BusinessLogicLayer_AreaSecurity _BusinessLogicLayer_AreaSecurity = new BusinessLogicLayer_AreaSecurity();
AreaSecurityModel _AreaSecurityModel = _BusinessLogicLayer_AreaSecurity
.GetAllAreaSecurity()
.ToList()
.FirstOrDefault();
ViewData.Add("AreaSecurity", _AreaSecurityModel);
}
}
base.OnActionExecuted(filterContext);
}
public ApplicationBaseController()
{
}
}
}
What I do not understand is that when I do _BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().ToList().FirstOrDefault() It does in fact return the list of all records.
However when I do _BusinessLogicLayer_AreaSecurity.GetAllAreaSecurity().GetAreaSecurityByUserID(user.Id) It returns the null value.
I have no idea what I am missing here. This is basically my first attempt at MVC code first.
I am ashamed to say that the problem is that my database for the security table is set as a Guid not a string.

How to store Texture2D in a listDictionary in C#?

I am having problems with retrieving a texture2D from a listDictionary.
This is my LoadGraphics Class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
namespace Space_Game
{
public static class LoadGraphics
{
//global variable
public static ListDictionary _blue_turret_hull;
public static void LoadContent(ContentManager contentManager)
{
//loads all the graphics/sprites
_blue_turret_hull = new ListDictionary();
_blue_turret_hull.Add("graphic", contentManager.Load<Texture2D>("Graphics/Team Blue/Turret hull spritesheet"));
_blue_turret_hull.Add("rows", 1);
_blue_turret_hull.Add("columns", 11);
}
}
}
This is the Turret class the should retrieve the Texture2D:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace Space_Game
{
class Turret_hull:GameObject
{
public Turret_hull(Game game, String team)
: base(game)
{
if(team == "blue") { _texture = LoadGraphics._blue_turret_hull["graphic"]; }
}
}
}
Only here it gives the following error:
Cannot implicitly convert type 'object' to 'Microsoft.Xna.Framework.Graphics.Texture2D'. An explicit conversion exists (are you missing a cast?)
I know there is a problem regarding the fact that I stored it in a listDictionary. I did that, because that way I could retrieve all necessary info at once. How else should I do this?
Thanks in Advance,
Mark Dijkema
ListDictionary is not generic, so the items are stored as Objects. You have to cast them back to the type you want:
if(team == "blue") { _texture = (Texture2D)LoadGraphics._blue_turret_hull["graphic"]; }
Probably a better way to achieve what you want is to define a new class with 3 properties and use this to retrieve the info you need to pass around:
public class TurretHullInfo
{
Texture2D Graphic { get; set; }
int Rows { get; set; }
int Columns { get; set; }
}
public static class LoadGraphics
{
//global variable
public static TurretHullInfo _blue_turret_hull;
public static void LoadContent(ContentManager contentManager)
{
//loads all the graphics/sprites
_blue_turret_hull = new TurretHull();
_blue_turret_hull.Graphic = contentManager.Load<Texture2D>("Graphics/Team Blue/Turret hull spritesheet");
_blue_turret_hull.Rows = 1;
_blue_turret_hull.Columns = 11;
}
}
class Turret_hull : GameObject
{
public Turret_hull(Game game, String team)
: base(game)
{
if(team == "blue")
_texture = LoadGraphics._blue_turret_hull.Graphic;
}
}

Categories