Explicit construction of entity type '###' in query is not allowed.

Explicit construction of entity type '###' in query is not allowed.


Issue

Explicit construction of entity type '###' in query is not allowed.

Solution


var result = from ev in db.Events
 join ea in db.EventAttendees on ev.EventId equals ea.EventId
 where ea.UserId == userid
 select new VMEvent // Here Don't use actual entity class name, Create new view model class
 {
  EventName = ev.EventName,
  EventAttendeeSubmittedDate = ea.SubmittedDate.ToString(),
  Place = ev.Place, 
  EventAttendeeNoofMembers = ea.noofMembers.ToString() 
  }; 

var arrayResult = result.ToArray();
var listResult = result.ToList();
 
  
Hope this will help you and save your time.

Enjoy !!!

:)

Unable to load one or more breakpoints in Visual Studio

Unable to load one or more breakpoints in Visual Studio


Issue

While I am opening the project, I got the error messag box "Unable to load one or more breakpoints".

Solution


I have resolved this issue by deleting the .vs directory and reopen visual stuio then the project.
  
Hope this will help you and save your time.

Enjoy !!!

:)

MongoDB with Credential in C#

MongoDB with Credential in C#




Open command prompt and goto C:\Program Files\MongoDB\Server\4.1\bin>


1.mongod --port 12345

2.mongo --port 12345

3.use admin;

4.db.createUser(
  {
    user: "admin",
    pwd: "admin143#",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
);

5.Restart mongodb instance

6.mongod --auth --port 12345

7.mongo --port 12345 -u "admin" -p "admin143#" --authenticationDatabase "admin"

8.use dbname

9.db.createUser(
  {
    user: "User1",
    pwd: "User1143#",
    roles: [ { role: "readWrite", db: "dbname" },
             { role: "read", db: "reporting" } ]
  }
);

Restart mongodb instance

10.  Install MongoDB Service
 mongod.exe --config "D:\MongoDB\demodb.conf" --service --install --serviceName "MongoDB DemoDB 12345" --serviceDisplayName "MongoDB DemoDB 12345" --auth 
 

 mongoDB usage with C# with credential

Sample code


//Create instance 
MongoUrl url = new MongoUrl("mongodb://username:password@hostname:port/demo");
MongoClient client = new MongoClient(url);
 
//Get Database 
var database = client.GetDatabase(url.DatabaseName);
 
//Read collection
IMongoCollection<Users> tempCollection  = database.GetCollection<classname>("collectionname");
 
//Result data
List<classname> resultData = tempCollection.AsQueryable().Where<classname>(a => a.propertyname == value).ToList();
 
  
Hope this will help you and save your time.

Enjoy !!!

:)

MongoDB with FilterDefinition in C#

MongoDB with C#



First you need to install "MongoDB.Driver" from nuget.

Code


MongoClient client;
           MongoUrl url = new MongoUrl("mongodb://localhost:PortNumber/databasename");
           client = new MongoClient(url);
 
           var database = client.GetDatabase(url.DatabaseName);
 
           FilterDefinition<CollectionClassName> filterQuery;
           var builder = Builders<CollectionClassName>.Filter;
 
           filterQuery = builder.And(
               builder.Eq("FieldName", NumericValue),
               builder.Eq("Field2Name""StringValue")
               );
 
           IMongoCollection<CollectionClassName> collectionResult = database.GetCollection<CollectionClassName
>("collectionName");
           var result = collectionResult.Find(filterQuery).ToList();

  
Hope this will help you and save your time.

Enjoy !!!

:)

Date conversion issue

Date conversion issue



Solution


string strDate = "dd/MM/yyyy HH:mm:ss";

try
{
 dateTime = DateTime.ParseExact(strDate, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
}
catch
{
 dateTime = DateTime.ParseExact(strDate, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
}

  

Hope this will help you and save your time.

Enjoy !!!

:)