Git Product home page Git Product logo

entityframeworkmongodb's Introduction

EntityFrameworkMongoDB

O código fornecido é um exemplo básico de uma aplicação em C# que ilustra o uso do Entity Framework para acesso a um banco de dados SQL Server e do driver do MongoDB para acesso a um banco de dados MongoDB.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using MongoDB.Driver;

namespace EntityFrameworkMongoDB
{
    public class Program
    {
        public static void Main(string[] args)
        {
            
            // Configurações de conexão com bancos de dados
            string sqlServerConnectionString = "sua-string-de-conexao-do-SQL-Server";
            string mongoDBConnectionString = "sua-string-de-conexao-do-MongoDB";

            // Exemplo de uso do Entity Framework (SQL Server)
            using (var dbContext = new DbContext(sqlServerConnectionString))
            {
                // Consulta utilizando LINQ
                var customers = dbContext.Customers.Where(c => c.IsActive).ToList();
                foreach (var customer in customers)
                {
                    Console.WriteLine(customer.Name);
                }
            }

            // Exemplo de uso do MongoDB
            var mongoClient = new MongoClient(mongoDBConnectionString);
            var mongoDatabase = mongoClient.GetDatabase("nome-do-banco-de-dados");

            var collection = mongoDatabase.GetCollection<YourEntity>("nome-da-colecao");
            var documents = collection.Find(Builders<YourEntity>.Filter.Empty).ToList();
            foreach (var document in documents)
            {
                Console.WriteLine(document.Name);
            }

           Console.ReadLine();
        }
    }

    // Exemplo de entidade para o MongoDB
    public class YourEntity
    {
        public string Id { get; set; }
        public string Name { get; set; }
        // Outros campos...
    }

    // Exemplo de entidade para o Entity Framework (SQL Server)
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsActive { get; set; }
        // Outros campos...
    }

    // Exemplo de DbContext para o Entity Framework (SQL Server)
    public class DbContext : Microsoft.EntityFrameworkCore.DbContext
    {
        private readonly string _connectionString;

        public DbContext(string connectionString)
        {
            _connectionString = connectionString;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(_connectionString);
        }

        public DbSet<Customer> Customers { get; set; }
        // Outros DbSets...
    }
}

entityframeworkmongodb's People

Contributors

ricardooliver avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.