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 !!!

:)

Partial View with Layout in MVC

Partial View with Layout in MVC


We can use partial view with layout page. Here is the sample code...


Model


public class Menu
{
 public string MenuText { get; set; }
 public string MenuUrl { get; set; }
 public string ToolTip { get; set; }
}

Controller

I have created a common controller and HeaderMenu action  to use server side code.


public ActionResult HeaderMenu()
{
 List<Menu> menu = new List<Menu>
 {
  new Menu(){ MenuText = "About", MenuUrl = "/Home/About", ToolTip = "About us"},
  new Menu(){ MenuText = "Contact", MenuUrl = "/Home/Contact", ToolTip= "Contact us"},
  new Menu(){ MenuText = "Products", MenuUrl = "/Products", ToolTip= "Product Catalog"}
 };

 return PartialView(menu);
}

View 

To use "HeaderMenu" partial view in Shared folder in Views. Change the Html code as per your design.


<div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            @Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav"> 
                @if (Model != null)
                {
                    foreach (var item in Model)
                    {
                        <li><a href="@item.MenuUrl" title="@item.ToolTip">@item.MenuText</a></li>
                    }
                }
            </ul>
        </div>
    </div>
</div>

Layout page

Finally I called partial view in layout page as below


<body>
     @Html.Action("HeaderMenu", "Common")
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div> 
</body>

Output



Hope this will help you and save your time.

Enjoy !!!

:)

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.


I got below error while calling web api on https and found the solution.

Error


System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

  

Solution

I solved this error by adding this line before calling the web method:

System.Net.ServicePointManager.ServerCertificateValidationCallback = (senderX, certificate, chain, sslPolicyErrors) => { return true; };


Hope this will help you and save your time.

Enjoy !!!

:)